Updated April 14, 2023
Introduction to Decorator in Python
Python decorators are a very useful tool in python and used to enhance the functions and classes’ functionality and behaviour. If we want to modify the behavior of the existing function without modifying the original function, then we can use decorators to alter its behavior, and we can also wrap another function. The function is used as an argument in the decorator, and then we can call it using a wrapper function, and it returns wrapper function as a result. So we are modifying or enhancing the behavior of the function without modifying the original function.
We use @ symbol to denote decorator is defined, and python will call the decorator first before calling the original function. Decorators works on four concepts:
- Nested function: It means we can one function inside another function and so on, as many we want.
- Python functions are capable of returning another function as output.
- Python functions are nothing much but a reference to another memory location.
- Python functions can also be used as an input parameter to the function.
Syntax:
def decorator_name(function_name):
def wrapper_function():
....
return wrapper_function
@decorator_name
def function_name():
Examples of Decorator in Python
Following are the examples of decorator in python:
Example #1
Code:
def decorator(func):
def wrapper_function():
print("Hello, How are you!")
func()
return wrapper_function
@decorator
def printMessage():
print("My name is John Doe!")
printMessage()
Output:
In the above program, we have defined a function printMessage that prints a message. If we want to add another message before this message, but we don’t want to make any changes in the original function, we have to define the decorator function.
We have defined a decorator function named a decorator, taking a function as an input argument. Now we have also defined a wrapper function that will print our desired message, and then it will call our original function. We have written our message, and then we have returned the wrapper function. Now we have to use @ to call the decorator function before our original function. When python reads @ symbol, it first calls decorator function then original function. You can notice we are returning wrapper_function without parentheses; it means we just return memory reference.
print(printMessage.__name__)
Output:
Now we want to check the name of the function which is executing. It will give output as “wrapper_function”. The decorator never shows the original function’s information like function name, parameter list, and docstrings.
Example #2 – Decorator using Parameter
Code:
def decorator(func):
def wrapper_function(x,y):
if(y==0):
return "Sorry number cannot be divided by zero"
return func(x,y)
return wrapper_function
@decorator
def divideNumber(x,y):
return x/y;
print(divideNumber(6,0))
Output:
In the above program, we have passed two-parameter x and y in the wrapper function, and we are performing division between them. We are checking if the value of the y is 0 or not. If the value is 0, then we are returning the custom message, and if the value is not zero, then the if condition will be ignored, and the operation will be carried out in the original function without any error. So we can say that we are catching error using a decorator; otherwise, python must have given an error.
Example #3 – Decorator using Class
Code:
def decorator(func):
def wrapper_function(username):
print("Hello, How are you! ",username.name)
func(username)
return wrapper_function
class Message:
def __init__(self, name):
self.name = name
@decorator
def printMessage(self):
print("My name is Alexa!")
obj = Message("John Doe")
obj.printMessage()
Output:
In the above program, we have created a class “Message”. We have created a constructor and passed a self and name parameter. We have created a method name as printMessage passed self keyword and print our message. Self refers to the instance of the class. We have created an object obj of the class and then call our function using the class object.
Conclusion
Decorator is a very useful feature, and it provides us with the option to improve the function of the existing function without modifying it. For using decorator, we use @ symbol and decorator name. It takes a function as an input argument and returns a function.
Recommended Articles
We hope that this EDUCBA information on “Decorator in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.