Python Decorators
Table Of Contents:
- What Are Decorators In Python?
- Examples Of Decorators.
- Using Multiple Decorators.
(1) What Are Decorators In Python?
- Decorators are just the usual methods used to add extra functionalities to an existing method.
- Python provides us with a way to add some extra functionality to an existing method, by using ‘Decorators’.
- Decorators actually don’t modify the codes inside the function, otherwise, it will cause serious security issues.
- Instead, it extends the function by adding some extra lines to it.
- Imagine a situation where you are using the methods from another file, that you don’t have access to modify.
- In that case, you can use the ‘Decorators’ to make changes to the method as per your requirements.
- It is like customizing your bike as per your choice.
(2) Examples Of Decorators.
Example-1
- Suppose you have a simple method that will take only two numbers.
- If you want to add some extra functionality, multiply these two numbers.
- Then you can use ‘Decorators’.
Existing Method:
def numbers(x,y):
print('Numbers Are:',x ,y)
Decorator Method:
def decorate(func):
def add(x,y):
func(x,y)
print('Addition:', x + y)
return add
Calling Decorator Method:
dec = decorate(numbers)
dec(20,30)
Output:
Numbers Are: 20 30
Addition: 50
Explanation:
- At first, you have a ‘numbers ( )’ method which takes two arguments and displays them.
- But we want to multiply these two numbers and display them.
- The restriction is that we can’t change the numbers( ) method because we don’t have permission to change the method.
- To solve this issue we can use ‘Decorators ‘ in our program.
- I have created the ‘decorator( )’ method which takes a function as an argument, which will be your ‘simple( )’ method.
- I have created another method inside it called the ‘add( )’ method which takes two arguments and here I am adding my extra functionality of multiplication.
- At last, I am returning the ‘add( )’ method, which is the new method with extra functionality.
Example-2
Simple Approach:
- Above is the common structure to declare ‘Decorators’ which is a little complex, Python has a syntax to simplify this.
- We can use the @ symbol along with the name of the decorator function, at the top of the function which you want to decorate.
- Then the decorator function will be applied to the existing function.
Decorator Method:
def decorate(func):
def add(x,y):
func(x,y)
print('Addition:', x + y)
return add
Existing Method:
@decorate
def numbers(x,y):
print('Numbers Are:',x ,y)
Calling Existing Method:
numbers(20,30)
Output:
Numbers Are: 20 30
Addition: 50
Example-3
Decorator Method:
def decorate(func):
def passfail(name,rollno,subject,mark):
func(name,rollno,subject,mark)
if mark > 30:
print('Student Is Passed')
else:
print('Student Is Passed')
return passfail
Existing Method:
@decorate
def students(name,rollno,subject,mark):
print('Name:',name)
print('Roll No:',rollno)
print('Subject:',subject)
print('Mark:',mark)
Calling Existing Method:
students('Abhispa',467,'Math',98)
Output:
Name: Abhispa
Roll No: 467
Subject: Math
Mark: 98
Student Is Passed
Note:
- At first, I have a ‘student( )’ method which prints the student details.
- I want to add extra functionality to show whether the student passed or failed.
- To do this I have created a decorator called ‘decorate()’ which is going to extend the functionality of the ‘student( )’ method.