Chain Multiple Decorators in Python
In this article, we will try to understand the basic concept behind how to make function decorators and chain them together we will also try to see Python decorator examples.
What is Decorator In Python?
A decorator is a function that can take a function as an argument and extend its functionality and return a modified function with extended functionality.
So, here in this post, we are going to learn about Decorator Chaining. Chaining decorators means applying more than one decorator inside a function. Python allows us to implement more than one decorator to a function. It makes decorators useful for reusable building blocks as it accumulates several effects together. It is also known as nested decorators in Python. we will also see Python decorator examples.
Syntax of decorator in python
@decor1 @decor def num(): statement(s)
For num() function we are applying 2 decorator functions. Firstly the inner decorator will work and then the outer decorator.
For sayhellogfg() and saygfg function, we are applying 2 decorator functions. Firstly the inner decorator(decor1) will work and then the outer decorator(decor2), and after that, the function datawill be printed.
************ @@@@@@@@@@@@ Hello @@@@@@@@@@@@ ************ GeekforGeeks
You can also learn about Genetator in Python.