22. Custom Decorators with Arguments
Custom decorators with arguments allow you to create more flexible and dynamic decorators by passing arguments to the decorator itself.
def repeat(n):
def decorator(func):
def wrapper(*args, **kwargs):
for _ in range(n):
func(*args, **kwargs)
return wrapper
return decorator
@repeat(3)
def say_hello():
print("Hello!")
say_hello()Hello!
Hello!
Hello!Last updated