Function decorators

 

Function decorators are the function that take a function as an argument and return a function. As per its name suggests it decorated a function that is passed as an argument.

Following points we must remember for function decorators:

  1. Decorator function is defined with another function as parameter.

  2. In decorator function another function is defined that decorates(modify) the value of function that is passed as parameter.

  3. The decorator function returns, inner function that has processed the value.

In following example ,function; décor is decorator function that modify the fun that has been passed as parameter inside funinside function and returns funinside.

Example:

def decor(fun):

def funinside():

ans=fun()

return(ans*5)

return funinside


def myfunction():

return(10)

finalans=decor(myfunction)

print(finalans())

Output:

50