Problem Statement: To understand Python Decorators.
In Python, decorators are powerful tools that modify the behavior of a function or class without modifying it permanently. It basically wraps another function and since both functions are callable, it returns a callable.
In simple words, we can say that A decorator wraps a function and modifies its behavior (not permanently).
FUNCTIONS
Before understanding decorators, let’s understand what a function is. A function returns a value based on the given arguments.
Example of a function:
Lets us understand about:-
- First-Class Objects
- Inner function
First-Class Objects:- In python, everything is treated as an object including all the data types, and functions too. Therefore, a function is also known as a first-class object and can be passed around as arguments.
Output:
Inner functions:- In python, it is possible to define functions inside a function. This property of function is called an inner function.
Output:
Decorators
Decorators can be extremely useful as they allow the extension of an existing function, without any modification to the original function source code. It has several usages in the real world like logging, debugging, authentication, measuring execution time, and many more.
Let’s understand by taking some examples
Example 1:
Explaining the use of decorators in python.
Solution:
Code:
Python Code
def decorator(func):
def TUF():
print("This article is written by")
func()
print("Intern at Takeuforward")
return TUF
def xyz():
print("Ayush Kumar")
xyz = decorator(xyz)
xyz()
Output:
This article is written by
Ayush Kumar
Intern at Takeuforward
Three functions are used in this program: TUF, decorator, and xyz.
decorator:
decorator function accepts another function as an argument and “decorates it” which means that it modifies it in some way and returns the modified version.
Inside the decorator function, we are defining another inner function TUF. This is the actual function that does the modification by the passed function func.
decorator function will return the TUF function.
xyz: This is a function that we need to decorate. Its work is to print a statement.
Syntax of decorators
xyz = decorator(xyz)
Here we can use @decoraters as well in place of the above syntax. The code with @decorator is below.
Code:
C++ Code
def decorator(func):
def TUF():
print("This article is written by")
func()
print("Intern at Takeuforward")
return TUF
@decorator
def xyz():
print("Ayush kumar")
xyz()
Output:
This article is written by
Ayush kumar
Intern at Takeuforward
Example 2 :
Now let’s code another problem where the program will reverse and convert the given string in uppercase and will display it on the screen.
String – “drawofuekat”
Solution:-
Try to solve it first , then look below
Python Code
def R_decorator(function):
def reverse():
do_reverse = "".join(reversed(function()))
return do_reverse
return reverse
def Up_decorator(function):
def uppercase():
make_uppercase = function().upper()
return make_uppercase
return uppercase
@Up_decorator
@R_decorator
def tuf():
return 'drawofuekat'
def main():
print(tuf())
if __name__ == "__main__":
main()
Output: TAKEUFOWARD
Special thanks to AYUSH KUMAR for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article. If you want to suggest any improvement/correction in this article please mail us at [email protected]