Decorators with Arguments
To pass arguments to a decorator, add an outer factory function that accepts the arguments and returns the actual decorator.
To pass arguments to a decorator, add an outer factory function that accepts the arguments and returns the actual decorator.
def repeat(n):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
for _ in range(n):
func(*args, **kwargs)
return wrapper
return decorator
@repeat(3)
def say_hi():
print("Hi!")
say_hi() # prints "Hi!" three times
A decorator with arguments is a three-level nested function.