Decorators in Python
In Python, decorators are a powerful and flexible feature that allows you to modify or extend the behavior of functions or methods without changing their actual code. Decorators are essentially higher-order functions that take a function as an argument and return a new function. They are denoted by the "@" symbol followed by the decorator name and placed above the function definition.
Creating a Basic Decorator
Let's define a simple decorator called my_decorator, which adds some additional functionality to a function.
In this example, we created a decorator my_decorator, which takes a function as an argument, wraps it with additional behavior (print statements in this case), and returns the modified function. We then applied the decorator to the say_hello function using the "@" syntax. When we call say_hello(), it actually calls the wrapper() function defined inside the my_decorator, effectively extending the behavior of the original say_hello function.
Decorators with Arguments
Decorators can also accept additional arguments if needed. To achieve this, we need to define a decorator factory, which is a function that returns a decorator.
In this example, we created a decorator factory repeat, which takes an argument n and returns a decorator. The returned decorator, when applied to the greet function with @repeat(3), calls the greet function three times.
Class-based Decorators
Decorators can also be implemented using classes. To create a class-based decorator, you need to define a class with a __call__() method, which will be called when the decorated function is called.
In this example, we created a class-based decorator MyDecorator, where the __init__() method is called when the decorator is applied, and the __call__() method is called when the decorated function is called.
Conclusion
Decorators are a powerful tool in Python, allowing you to add functionality, logging, validation, and more to functions and methods without modifying their original code. They promote clean and modular code by separating concerns and making the code more reusable and maintainable.
- Keywords in Python
- Python Operator - Types of Operators in Python
- Python 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