Decorators in Python
Python decorators are a powerful and useful tool in Python, allowing developers to modify or extend the behavior of a function, method, or class, without changing the source code. It is a special type of function that takes a function or class as an argument and returns a modified version of it, which can then be used as the original function or class.Python Decorators | example
Following is a simple example to illustrate how decorators work:
def squre_decorator(func):
def f_inner():
in_val = func()
modified_val = in_val * in_val
return modified_val
return f_inner
def squre():
return 2
squre = squre_decorator(squre)
print(squre()) # OUTPUT: 4
In this example, the squre_decorator function is a decorator that takes a function func as an argument and returns a new function f_inner, which is a modified version of func. The f_inner function calls the original function func, gets its result, modifies it by calculating squre of value, and returns the modified result. The squre function is then decorated with squre_decorator, effectively replacing it with the f_inner function.
@ Symbol With Python Decorator
You can also use the @ syntax to apply a decorator to a function, like so:
def squre_decorator(func):
def f_inner():
in_val = func()
modified_val = in_val * in_val
return modified_val
return f_inner
@squre_decorator
def squre():
return 10
print(squre()) # OUTPUT: 100
Decorators and classes
Decorators can also be applied to classes, in which case they modify the behavior of the class itself, or to class methods, in which case they modify the behavior of a specific method.
In addition to modifying the behavior of functions and classes, decorators can also be used for other purposes, such as logging, timing, or caching. By taking advantage of Python's decorators, you can write code that is more modular, easier to maintain, and more flexible.
Add Arguments to Decorators in Python
You can add arguments to decorators by defining the decorator as a function that takes arguments, and then returning another function that takes the original function as an argument. The returned function is then used to modify the behavior of the original function.
def repeat_and_add(num_repeats, add_value):
def decorator(func):
def wrapper(*args, **kwargs):
result = 0
for i in range(num_repeats):
result += func(*args, **kwargs)
return result + add_value
return wrapper
return decorator
@repeat_and_add(num_repeats=3, add_value=5)
def add_two_numbers(a, b):
return a + b
result = add_two_numbers(1, 2)
print(result) # OUTPUT: 14
In the above example, the repeat_and_add decorator factory takes two arguments: num_repeats and add_value. The decorator function takes the func argument and returns the wrapper function, which calls the func argument num_repeats times and adds the add_value to the result. The wrapper function is essentially a modified version of the original function that has the additional behavior specified by the decorator.
When the add_two_numbers function is decorated with the repeat_and_add decorator and called with arguments 1 and 2, the output will be: 14
How to get the output 14?
=(1+2) + (1+2) + (1+2) + 5
=3+3+3+5
=14
As you can see, the add_two_numbers function has been called three times, and the result of each call has been added together. Finally, the add_value of 5 has been added to the final result.
Advantages of python decorators
There are several advantages to using decorators in Python:- Code Reusability: Decorators allow you to reuse code across multiple functions, making your code more maintainable and easier to update.
- Functionality Composition: Decorators can be combined to create complex behavior, making it easy to build powerful abstractions from simple building blocks.
- Modularity: Decorators can be written and maintained separately from the functions they modify, making it easier to evolve the behavior of a system over time.
- Readability: Decorators can help to make code more readable by making the intent of the code clearer. For example, a decorator that adds timing information to a function makes it easier to understand the performance of the function.
- Dynamic Behavior: Decorators allow you to add or remove behavior at runtime, making it possible to change the behavior of a system dynamically.
- Debugging: Decorators can help to debug code by providing additional information about the state of the system, such as timing information or logging output.
When is the Appropriate Time to Utilize a Decorator in Python?
Decorators in Python are a powerful tool that allow you to modify or extend the behavior of functions and classes. There are several scenarios where it might be appropriate to use a decorator:- To add or modify the functionality of a function or class without changing its source code.
- To apply the same behavior to multiple functions or classes by wrapping them in a single decorator.
- To add timing, logging, or other debugging information to a function or class.
- To enforce access restrictions or security checks for a function or class.
- To add metatdata to a function or class that can be used for introspection or other purposes.
Related Topics
- Keywords in Python
- Python Operator - Types of Operators in Python
- Python Variables and Data Types
- Python Shallow and deep copy operations
- Python Datatype conversion
- Python Mathematical Function
- Basic String Operations in Python
- Python Substring examples
- How to check if Python string contains another string
- Check if multiple strings exist in another string : Python
- Memory Management in Python
- Python Identity Operators
- What is a None value in Python?
- How to Install a Package in Python using PIP
- How to update/upgrade a package using pip?
- How to Uninstall a Package in Python using PIP
- How to call a system command from Python
- How to use f-string in Python
- Python Timestamp Examples