For example, say you have a function to capitalize the first letter of names. Now you want to add another function on top of this to include the title (Mr/Ms) depending upon the gender. This can be achieved by defining a decorator function to add the title.
Decorator Function to add the title (Mr/Ms) based on gender:
def new(*args, **kwds):
if kwds['gender'] == 'male': return ("Mr "+func(*args, **kwds))
elif kwds['gender'] == 'female': return ("Ms "+func(*args, **kwds))
else: return func(*args, **kwds)
Regular function to capitalize the first letter with decorator:
Now call the regular function as below:
result >>> 'Mr John Travolta'
Capitalize('catherine zeta jones',gender='female')
result >>> 'Ms Catherine Zeta Jones'