Thanks to visit codestin.com
Credit goes to flexiple.com

Flexiple Logo
  1. Home
  2. Blogs
  3. Python
  4. Decorators With Parameters In Python

Decorators With Parameters In Python

Author image

Harsh Pandey

Software Developer

Published on Tue Mar 12 2024

Decorators with parameters in Python enhance the decorator's functionality by allowing it to accept arguments. This feature enables more dynamic and flexible decorator behaviors, as it can be customized per use case. Parameters are passed to the decorator, allowing it to perform operations based on these arguments before applying the decoration logic to functions or methods, thereby extending their capabilities with additional, customizable logic.

The Syntax For Decorators With Parameters In Python

The syntax for decorators with parameters in Python involves creating a wrapper around the decorator itself, allowing it to accept arguments. To implement a decorator with parameters, you define a decorator factory, which is essentially a function returning a decorator. The returned decorator then wraps the target function. This nested structure enables passing parameters to the decorator.

Here's a basic example.

def decorator_with_args(arg1, arg2):
    def decorator(func):
        def wrapper(*args, **kwargs):
            print(f"Decorator arguments: {arg1}, {arg2}")
            return func(*args, **kwargs)
        return wrapper
    return decorator

@decorator_with_args('Hello', 'World')
def say_hello(name):
    print(f"Hello {name}")

say_hello("Python")

Output:

Decorator arguments: Hello, World
Hello Python

This code demonstrates a decorator decorator_with_args that accepts parameters arg1 and arg2. The say_hello function is wrapped by this decorator, allowing it to print additional information before executing the function's original logic. Through this approach, decorators become highly versatile, capable of influencing the behavior of the functions they decorate based on the parameters they receive.

How Is Decorator With Parameters Implemented?

Implementing a decorator with parameters in Python involves creating a wrapper function around the original decorator, enabling it to accept arguments. This technique allows decorators to be more flexible and adaptable to various use cases by passing custom parameters at the time of decoration.

To achieve this, define a decorator function that returns another decorator function, which in turn wraps the original function. The outermost function takes the parameters for the decorator, while the inner function uses these parameters to modify the behavior of the original function.

Example.

def repeat(times):
    def decorator(func):
        def wrapper(*args, **kwargs):
            for _ in range(times):
                result = func(*args, **kwargs)
            return result
        return wrapper
    return decorator

@repeat(times=3)
def say_hello(name):
    print(f"Hello, {name}!")

say_hello("Alice")

Output:

Hello, Alice!
Hello, Alice!
Hello, Alice!

This code demonstrates a decorator, repeat, which accepts a parameter times. It repeats the execution of the say_hello function based on the number of times specified, showcasing the flexibility of decorators with parameters.

Related Blogs

Browse Flexiple's talent pool

Explore our network of top tech talent. Find the perfect match for your dream team.