Python Decorators
Python decorator is a callable object-like function, it is used to modify a function and class. A reference to a function “func” and class “C” pass to decorator and it will return a modified function and class.As we have studied that, we can use the argument of function inside the function.
In this tutorial, we are going to learn about the decorators step by step with Examples. Learn Python Decorators with arguments, and Python Decorators without arguments.
Syntax of Decorator
def my_decorator(f):
Here, my_decorator is the decorator and f is the parameter passed in the function, which is also a function.
Why we use Decorators
Decorators are used to provide additional functionality to the Functions without modifying the code of the Function. We can also modify the functionality with the help of Decorators.
Example of Decorator
def my_decorator(func): # creating decorator
def wrap():
print("This is inside the decorator before calling the function")
func()
print("This is inside the decorator after calling the function")
return wrap # decorator needs to return a function
@my_decorator # using decorator
def print_Name():
print("India")
printName() # Calling the function
This is inside the decorator before calling the function
India
This is inside the decorator after calling the function
How to use Python Decorators
Let’s understand the Decorators working with some simple steps.
Step – 1: Creating a simple Function.
Let’s simply create a function that will print a name.
def printName():
print("India")
Step – 2: Defining a Decorator.
Decorators are like the Function, and it takes a function as an argument
#this is the decorator
def my_decorator(func):
# simple wrote a function which will print the name
def print_Name():
print("India")
Now, we need to use that argument and add some functionality inside the decorator.
Step – 3: Creating a Decorator with some functionality.
Here we are going to define a function inside the Decorator. And call the argument of function inside that function.
def my_decorator (func):
def wrap():
print("This is inside the decorator before calling the function")
func()
print("This is inside the decorator after calling the function")
return wrap # decorator needs to return a function
def print_Name():
print("India")
We defined a function wrap() inside the Decorator. And then we called the argument of Decorator i.e. func inside the wrap() function. At last, the decorator returns the wrap() function.
Step – 4: Adding Functionality to the Original Function.
As we have created a Decorator. Now, we need to use the Decorator to modify the Functionality of Decorators.
def my_decorator(func):
def wrap():
print("This is inside the decorator before calling the function")
func()
print("This is inside the decorator after calling the function")
return wrap # decorator needs to return a function
@my_decorator # using decorator with print_Name() Function
def print_Name():
print("India")
Now, whenever we call the print_Name()
function, the Decorator will be called first. Let’s try to understand the Functionality of Decorator.
What the decorator will be going to do is that, Whatever we will return by the decorator it will reassign to the print_Name()
function. And then again the wrap(
) function.
Now, let’s see what happens when we call the original Function i.e. print_Name()
.
This is inside the decorator before calling the function
India
This is inside the decorator after calling the function
Decorators with the arguments
Above, we have used the Decorators without the arguments.
Now we can easily pass the arguments to the Decorators.
Example of decorator with arguments
def my_decorator(msg='hi'):
def this_is_Decorated(func):
# *args denoate that the argument passed in the function is keyword argument
def wrapper():
print(msg)
print("Inside the decorator, before calling the function")
func()
print("Inside the decorator, after calling the function")
return wrapper # decorator needs to return a function
return this_is_Decorated
@my_decorator(msg='hello')
def prin_tName():
print("India")
printName()
hello
Inside the decorator, before calling the function
India
Inside the decorator, after calling the function
Conclusion
Decorators take the original functional and enhance the original function without modifying.
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |