Python Decorators

A Decorator is a function that takes a function as its argument and extends its functionality and returns modified function with extended functionality.

Every function in python is treated as an object, the following example demonstrates decorator

def wish(name):
    print("Good Morning..:",name)
greeting=wish 
print(id(wish))
print(id(greeting))

In the above example, we can consider wish as a function as well as an argument, now an object got created and the wish is pointing to that object. When we assign wish to greeting, the greeting is also pointing to the same object, and this is also known as function aliasing.

Now, we have one object and two reference variables; if we print id of wish and id of greeting, both are pointing to the same address.

id-of-wish-and-greeting-are-same

We can access the function by using wish and greeting reference variables

def wish(name):
    print("Good Morning..:",name)
greeting=wish 
wish("Anvitha")
greeting("Nakul")

The output is:

aceesing-the-function-by-using-two-reference-variables

We can access the function object by using another reference variable/alias name If we delete one reference variable.

def wish(name):
    print("Good Morning..:",name)
greeting=wish 
del wish
greeting("Navya")

The output is:

delting-one-refernce-variable-and-acessing-by-another-reference-variable

The Readers who are reading Python Decorator for the first time should also read the below topics
Nested Functions
A Function as a return value or A Function as an Argument to the Function

The Advantages and Limitations of Generator

Decorator Chaining

Defining the multiple decorators for the same function is called as decorator chaining.

The following example demonstrates the decorator chaining

def decor1(func):
    def inner1():
        print("Decorator1 execution")
    return inner1

def decor2(func):
    def inner2():
        print("Decorator2 execution")
    return inner2

@decor2
@decor1
def function():
    print("Original function")

function()    

The output is:

decorator-chaining

We can call the original function from the decorator2, but whenever we try to call the original function from the decorator2 the recursion error will occur

def decor1(func):
    def inner1():
        print("Decorator1 execution")
    return inner1

def decor2(func):
    def inner2():
        print("Decorator2 execution")
        function()
    return inner2

@decor2
@decor1
def function():
    print("Original function")

function()    

The output is:
File "C:UsersUserAnaconda3lib	hreading.py", line 1048, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):

RecursionError: maximum recursion depth exceeded while calling a Python object

The following example demonstrates the decorator chaining

def decor1(func):
    def inner1():
        x=func()
        return x*x
    return inner1

def decor2(func):
    def inner2():
        x=func()
        return 2*x
    return inner2

@decor2
@decor1
def num():
    return 20

print(num())a

The output is :

decorator-chaining-example

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions