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.
1. Decorator with a Simple Argument
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()Output:
Hello!
Hello!
Hello!2. Logging Decorator with a Custom Message
Output:
3. Timing Decorator with a Label
Output:
4. Access Control Decorator with a Role
5. Rate Limiter with a Custom Limit
6. Dynamic Prefix Decorator
Output:
7. Validation Decorator with Custom Error Message
8. Custom Logger Decorator with Log Levels
Output:
9. Retry Decorator with Retry Count
10. Environment-Specific Execution Decorator
Last updated