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.
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.
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.