Junior — Middle
Have you used decorators that accept parameters?
sobes.tech AI
Answer from AI
Yes, I have used parameterized decorators in Python. Such decorators are functions that accept arguments and return the decorator itself. This allows customizing the behavior of the decorator when applied.
Example of a simple decorator with a parameter:
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()
In this example, the repeat function takes a times parameter and returns a decorator that calls the function multiple times.