Python decorators are a powerful tool that allows you to modify functions or methods, adding extra functionality without changing the original code. They are widely used for tasks such as logging, timing, or wrapper functions. In this guide, we'll delve into the world of Python decorators, providing examples and explaining their syntax in detail.

Decorators provide a clean and elegant way to modify the behavior of functions or methods, making your code more readable and maintainable. They are a key aspect of Python's dynamic nature and are extensively used in frameworks like Django and Flask.

Understanding Function Decorators
Before we dive into examples, let's understand what function decorators are. In its simplest form, a decorator is a function that takes another function as an argument and returns a new function that wraps the original function.

Decorators use the '@' operator to wrap functions, making the decoration process more readable. At the call site, you simply place the decorator name above the function you want to modify.
Basic Syntax of Decorators

Here's the basic syntax of a decorator in Python:
def decorator_function(func):
return wrapper_function
@decorator_function
def original_function():
pass
The '@' symbol tells Python to apply the decorator to the function below it. In this case, the 'original_function' will be replaced with whatever 'wrapper_function' returns.
Example: Timing Functions

Decorators can be used to time functions. Here's an example:
import time
def timer(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
print(f"{func.__name__} took {end_time - start_time} seconds to execute.")
return result
return wrapper
@timer
def hello_world():
time.sleep(2)
return "Hello, World!"
When you call hello_world(), it will print how long it took the function to execute, which in this case is 2 seconds due to the 'time.sleep(2)' line.
Decorating Functions with Arguments

Decorators can also accept arguments, allowing for more complex behavior. They can be defined to accept arguments using the '*' and '**' operators, which pack and unpack arguments into tuples and dictionaries respectively.
Positional Arguments with Decorators


















Here's how you can accept positional arguments in a decorator:
def add Deuxième_arg(func):
def wrapper(*args, **kwargs):
print("Second Argument")
return func(*args, **kwargs)
return wrapper
@add_Déuxième_arg
def hello_world2(arg):
print(f"Hello, World! {arg}")
In this case, the decorator 'add_Déuxième_arg' takes a function as an argument and adds a positional argument to it.
Keyword Arguments with Decorators
You can also use keyword arguments in decorators. Here's an example:
def add_kwargs(func):
def wrapper(*args, **kwargs):
print("Keyword Arguments")
return func(*args, **kwargs)
return wrapper
@add_kwargs
def hello_world3(keyword_arg):
print(f"Hello, World! {keyword_arg}")
In this case, the decorator 'add_kwargs' accepts a function and adds a keyword argument to it.
Decorating Functions with Multiple Decorators
Python allows you to stack decorators, applying multiple decorators to a function. You can stack decorators vertically, with each decorator applying its behavior in the order they are defined.
Stacking Decorators
Here's an example of stacking decorators:
@timer
@add_Déuxième_arg
@add_kwargs
def hello_world4(arg, keyword_arg):
time.sleep(2)
return f"Hello, World! {arg}, {keyword_arg}"
hello_world4("World", " nuts=")
In this case, the function 'hello_world4' is decorated with 'timer', 'add_Déuxième_arg', and 'add_kwargs'. The output will be: "Second Argument", "Keyword Arguments", followed by the function execution time.
In conclusion, Python decorators are a powerful tool that can significantly improve your coding experience. They allow you to modify functions on the fly, making your code more dynamic and readable. Whether you're timing functions, adding arguments, or performing complex behaviors, decorators have you covered. Start experimenting with decorators today and elevate your Python skills to the next level!