Nested Decorators in Python
Python functions are first-class objects in the Python programming language. It means that a function can be assigned to a variable, return another function, and, most importantly, take another function as an argument. The concept of the Python decorator is based on these features of functions.
It is assumed that you have a basic understanding of Python decorators. If you aren't familiar with decorators, you can learn from our decorators in the Python tutorial. In this tutorial, we will learn about the nested decorators or chaining of the decorators.
Nested Decorators in Python
Everything in Python is an object, and each object has it associated class in Python. Python decorators are used to modifying the function's behavior without changing its actual value. The decorator is the same as suggesting itself - which decorates something.
Nested decorators are simple as the normal decorators. Nesting means placing or storing inside the other. Therefore, Nested decorators mean applying more than one decorator inside a function. Python provides the facility to implement more than one decorator to a function. It makes decorators useful for reusable building blocks as it consists of several features together.
How are Nested Decorators used?
A function can be decorated multiple times. The nested decorators are also known as the chaining of the decorators. To create the nested decorator, first, we need to define the decorator that we want to wrap the output string with and apply them to function using the pie syntax (@ sign). Let's understand the following syntax.
As we can see in the above syntax, there are two decorators for a particular approach. These decorators will be executed in the bottom to top approach i.e. reverse order. We can take the reference of the construction of the building where we start the construction from the ground to then build floors.
Let's understand the following example.
this is a basic program of nested decorator.
In the above code, we have defined two decorator functions first, which are used to wrap the output string of the decorator function in the 'lower()' and 'upper()' function of the string.
Example - 2:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ # # # # # # # # # # # # # # Hello # # # # # # # # # # # # # # $ $ $ $ $ $ $ $ $ $ $ $ $ $
Nesting Parameterized Decorators
Now, let's implement the nested parameterized decorator where the method takes arguments. Here, we will create the two parameterized decorators - One will perform the multiplication of the two parameters, and the second will perform the divide. Let's see the following example.
Let's understand what we have done in the above code -
The above code works same as the previous examples; the decorator could be applied inside a method as well.
Decorators are one of the best features of the Python programming language. In this tutorial, we have covered the advanced concept of decorators. We have explained simple and parameterized decorators both.
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week