Sobes.tech
Junior — Middle

Can you explain what a parameterized decorator is and how it is used?

sobes.tech AI

Answer from AI

A parameterized decorator is a decorator function that takes arguments and returns the decorator itself. Such a decorator allows customizing the behavior of the wrapped function.

Example of a parameterized decorator in Python:

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():
    print("Hello")

say_hello()

Here, repeat is a parameterized decorator that repeats the function call a specified number of times.