Introduction
In this article, you will learn about Python Decorators, different types of Python decorators, and how we can use them in Python.
Python
Python is an interpreted, high-level, general-purpose programming language created by Guido van Rossum and first released in 1991. He started Python as a hobby project to keep him occupied in the week around Christmas. It got its name from the name of the British comedy troupe Monty Python. It is used in:
- Software Development
- Web Development
- System Scripting
- Mathematics
To learn how to program in Python, visit Python Basics.
You can visit C# Corner Python Learn Series.
Python Decorators
A decorator is a Python design pattern that allows a user to add additional functionality to an existing object without changing its structure. Decorators are often summoned prior to the specification of the function to be decorated. These are used to alter the function's behavior. Decorators allow you to wrap another function in order to extend the functionality of the wrapped function without permanently changing it.
Decorators have several use cases such as:
- Authorization in Python frameworks such as Flask and Django
- Logging
- Measuring execution time
- Synchronization
Why are decorators useful?
Decorators are an important programming pattern and if used wisely, can provide a lot of benefits. It makes code very reusable and binds added functionality to functions, hence keeping code DRY.
Given below is an example, how we can use decorators in Python.
The above "authenticated" decorator function only invokes the message_friends function based on the specified condition. This gives a lot of flexibility and performs conditional operations based on the status of the user’s authentication.
Defining Decorators
There are various ways in which we can utilize decorators in python.
1. Decorating functions with parameters
Here we pass the function, which needs to be decorated, to the decorator function. After which we make the required function call, with the parameter, to produce the output.
The output of the code will be 1.
2. Using Syntactic Decorator (@)
Here instead of passing the function to be decorated to the decorator function, we write "@" followed by the decorator function name before the function definition (which needs to be decorated).
The output of the code will be 1.
3. Using Decorators between files
We can define a decorator in one python file, and use it in another file. Let's take an example, to better understand.
We write the above code, in test.py.
The above code, when executed results in "CSHARPCORNER" as output.
4. Applying multiple decorators to a single function
In the above code, we apply two decorators to the print_c(). And when we print, we get ['CSHARP', 'CORNER'].
5. Passing arguments to Decorators
In the above code, we pass parameters to the deco_r() which then will be passed to inner(). The output of the code will be 1.
6. General-Purpose Decorators
args and **kwargs are used to define a generic decorator that can be applied to any function. All positional and keyword arguments are collected and stored in the args and **kwargs variables. args and kwargs let us send as many arguments as we like during function calls.
Following is a general-purpose decorator, which prints the passed positional and keyword arguments.
Now we will test this decorator, using the following,
-
Without Positional and Keyword Arguments
The output of the above code will be,
The positional arguments are ()
The keyword arguments are {}
No arguments here.
-
Without Keyword Arguments
The output of the above code will be,
The positional arguments are (1, 2, 3)
The keyword arguments are {}
1 2 3
-
Without Positional Arguments
The output of the above code will be,
The positional arguments are ()
The keyword arguments are {'first_name': 'CSharp', 'last_name': 'Corner'}
This has shown keyword arguments
7. Class as decorator
To create a decorator as a class, we must utilize a class's __call__ function. When a user wishes to build an object that functions as a function, the function decorator must return an object that behaves like a function, which is where __call__ comes in.
The output of the above code will be "C# CORNER".
The above code demonstrates, how we can create and use a class decorator. One can add any code inside the __call__(), both before and after the function call.
I have used the same logic for both of them so that you get a better understanding of the concept.
Conclusion
In this article, we discussed what are Python Decorators, different types of Python decorators, and how we can use them in Python. Do try out the code and make tweets to enhance your understanding, and comment with your views on how useful this article was.
Visit C# Corner to find answers to more such questions.
If you have any questions regarding any other technology do have a look at the C# Corner Technology Page.