decorator¶
- decorators are a design pattern that allows you to modify the behavior of functions or classes without permanently altering their source code.
- They provide a clean and concise way to add functionality or alter behavior in a reusable manner.
- Decorators are essentially functions that take another function (or class) as an argument and return a modified version of it.
- The modified function can have additional functionality before, after, or even instead of the original function's execution.
- Creating Decorators
- Decorators are defined as regular Python functions.
- They typically take a function as an argument and return another function (the modified version).
In [ ]:
Copied!
def my_decorator(func):
def wrapper():
print("Something before the function is called")
func()
print("Something after the function is called")
return wrapper
@my_decorator
def say_hello():
print("Hello from the decorated function!")
say_hello() # Output:
# Something before the function is called
# Hello from the decorated function!
# Something after the function is called
def my_decorator(func):
def wrapper():
print("Something before the function is called")
func()
print("Something after the function is called")
return wrapper
@my_decorator
def say_hello():
print("Hello from the decorated function!")
say_hello() # Output:
# Something before the function is called
# Hello from the decorated function!
# Something after the function is called
- Explanation
my_decorator
takes a function (func
) as an argument.- It defines a nested function
wrapper
that encapsulates the additional behavior. wrapper
callsprint("Something before the function is called")
before executing the original function (func()
) usingsay_hello()
in this case.- After the original function call, it executes
print("Something after the function is called")
. - Finally,
wrapper
is returned frommy_decorator
.
- Using the
@
Symbol- The
@
symbol is a convenient syntax for applying decorators. - You place the decorator function name above the function you want to decorate, like
@my_decorator
in the example. - This is equivalent to calling
my_decorator(say_hello)
and assigning the result tosay_hello
.
- The
- Common Use Cases
- Logging: Decorators can be used to log the execution of functions, recording information about function calls and their results.
- Authentication and Authorization: Decorators can be used to verify user permissions before allowing access to certain functions.
- Error Handling: Decorators can handle potential errors within functions and provide a centralized approach to error management.
- Caching: Decorators can be used to cache function results, improving performance by avoiding redundant calculations.
- Performance Measurement: Decorators can be used to measure the execution time of functions, helping identify performance bottlenecks.
- Key Points
- Decorators provide a flexible way to modify function (or class) behavior without changing their source code.
- They promote code reusability by encapsulating common functionality in decorators.
- The
@
syntax offers a concise way to apply decorators.