.jpg)
In other words, a decorator in Python is a way to modify or enhance the behavior of a function or class by wrapping it with another function. This can be useful in a variety of situations, such as:
Here's an example of a simple decorator in Python:
In this example, my_decorator is a function that takes another function as input and returns a new function that wraps the original function. The wrapper function adds some extra functionality before and after the original function is called. The @my_decorator syntax is a shorthand way of applying the decorator to the say_hello function.
When say_hello() is called, the output will be:
As you can see, the output of the say_hello function has been modified by the decorator.
- Adding authentication or logging to a function
- Timing the execution of a function
- Caching the results of a function
- Adding error handling to a function
Here's an example of a simple decorator in Python:
def my_decorator(func): def wrapper(): print("Before function is called") func() print("After function is called") return wrapper @my_decorator def say_hello(): print("Hello World") say_hello()
In this example, my_decorator is a function that takes another function as input and returns a new function that wraps the original function. The wrapper function adds some extra functionality before and after the original function is called. The @my_decorator syntax is a shorthand way of applying the decorator to the say_hello function.
When say_hello() is called, the output will be:
Before function is called Hello World After function is called
As you can see, the output of the say_hello function has been modified by the decorator.
0 Comments