Python Decorator
Advertisement
In this blog, we will learn about Python Decorator and understand each and every concept with the help of examples.
If you have not read the previous python blog, must-read python generators that create an iterator object that generates values on the fly.
To better understand Python Decorators, you should have a good knowledge of the basics of inner or nested function in python and closures in python.
What is a Decorator in python?
In English terms, Decorator is means a thing that is used for decoration. In python too, A Decorator is used to decorate a function.
A Decorator takes a function and returns the decorated function to us.
First Let’s understand how we can decorate a function using the inner function and passing function as an argument. After this example, we will learn a more easy way to do it.
Python inner function as a Decorator
The below code is the implementation of Decorator using Python inner function.
def greet(func):
def wish():
print(f"Hello, How are you? {func()}")
return wish
def nameing():
return "HTD"
nameing = greet(nameing)
nameing()
Let’s Understand the program.
The greet function takes a function and implements an inner function, which simply prints a greeting and executes the function passed as a parameter (func
). The greet function then returns the inner function (wish
in this case)
Next, we create a different function which we want to decorate using the greet function (naming
in this case).
In the next step, the naming function is assigned with the greet function that takes the naming function as an argument. Then we execute the naming function. And Hence we get a decorated function.
Hello, How are you? HTD
This process was big and also a bit confusing. Let’s learn an easy way to do the same using python decorators.
Python Decorator
The Below code is an example of a Python Decorator.
def greet(func):
def wish():
print(f"Hello, How are you? {func()}")
return wish
@greet
def naming():
return "Hack The Developer"
naming()
Hello, How are you? Hack The Developer
This time also we had to create two functions, one the decorator function and the other the function to be decorated.
@greet
def naming():
return "Hack The Developer"
In the previous section, we assigned a function to others to achieve the decoration functionality. But this time we are using a @
symbol with the decorator function over the function to be decorated.
Multiple Python Decorators
We can also use Multiple Python Decorators to decorate a single function.
Example:
def morning(func):
def wish():
print(f"{func()}")
return wish
def greet(func):
def wish():
print(f"Hello, How are you? {func()}")
return "Greeting Done"
return wish
@morning
@greet
def naming():
return "HTD"
naming()
The decorator that is closest to the function to be decorated runs first and then the above decorators.
In this case, the @greet
decorator will run first and then the @morning
decorator.
Let’s see the output:
Hello, How are you? HTD
Greeting Done
Hope you like it!