Decorator Pattern
Decorator Pattern is a reusable Python snippet. Decorators modify the behavior of functions or methods. They are widely used for logging, timing, authentication, and caching. The full implementation: import time from functools import wraps # Basic decorator def timer(func): @wraps(func) def wrapper(*args, **kwargs): start = time.time() result = func(*args, **kwargs) end = time.time() print(f"{func.__name__} took {end-start:.2f}s") return result return wrapper @timer def slow_function(): time.sleep(1) return "Done" slow_function() # "slow_function took 1.00s" # Decorator with arguments def repeat(n): def decorator(func): @wraps(func) def wrapper(*args, **kwargs): for _ in range(n): func(*args, **kwargs) return wrapper return decorator @repeat(3) def say_hello(): print("Hello!") It belongs to the Functools Module area of the standard library. This snippet uses only the Python standard library with no third-party dependencies, so it runs on any modern Python 3 interpreter. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.