Decorators in Python

Decorators in Python

Decorators in Python are functions that accept a function as a parameter and return a function. You can use a decorator to get the result of a function, update the result, and return it. You might find the concept of Function Decorators in Python a little confusing, but it is not rocket science at the end of today to understand either.

Decorators in Python – How to create?

Please note down the steps that are involved in creating decorators:

Step 1: We should define a decorator function that has a parameter which is the name of another function.

def decor(fun):

Step 2: We should define an inner function within the decorator function that will decorate or modify the value of the function passed to the decorator function.

def decor(fun):
    def inner():
       value=fun() #access the value returned by fun()
        return value+3 #add the value by 3
    return inner

In the code above, we added a function called inner() within the decorator function. This inner function has a variable value that accesses the result of the function fun(). In the next line of code, we increased the same value by 3 and eventually returned the inner function.

Step 3: In the previous two steps, we defined a decorator function and also added an inner function within it to modify the value of any function that is passed as a parameter to it. It means you can now pass any function to this decorator function. Let us declare another function now for the same.

def num():
    return 7
result_fun = decor(num)

In the above piece of code, we declared a function num and passed the same to decor function and stored its result in the variable result_fun. Since the num function contains the value 7, it gets incremented by 3 when it is passed through the decor function, and the final value 10 gets stored in the result_fun. You can use print(result_fun) to display the result.

Let us now look at a program to understand this:

#declaring a decorator function
def decor(fun):
    def inner():
        value=fun()
        return value+3
    return inner

#get a function to apply the decorator
def number():
    return 7

#call the decorator function
result=decor(number)
print(result())
Output

10

We can apply a decorator to any function using the symbol ‘@’ followed by the decorator name. Since the name of the decorator function we’ve declared is decor, we can use it as @decor. Let us now rewrite the same program using this concept.

#declaring a decorator function
def decor(fun):
    def inner():
        value=fun()
        return value+3
    return inner

@decor #apply decor to the function below
def number():
    return 7

print(number())
Output

10

Let us look at one more program that deals with two decorators:

#decorator that increments the value of function by 3
def decor(fun):
    def inner():
        value=fun()
        return value+3
    return inner

def decor1(fun):
    def inner():
        value=fun()
        return value*3
    return inner

#apply decorators
@decor
@decor1
def num():
    return 10

print(num())
Output

33

In the above program, we added two decorators decor and decor1. You know that the decor function contains the logic to increase the value by 3. And in the newer decor1 function, we have added a function to multiply the value by 10. Since two decorator functions are there in the program, decor1 is first applied to the num() function followed by the decor function. If you want this processing to happen the other way around, you have to swap the positions of decor and decor1 above the num() function.

Scroll to Top