Basic Decorator
def my_dec(func):
def wrapper(*a, **kw):
return func(*a, **kw)
return wrapper | Basic decorator pattern |
@my_dec
def hello(): pass | Apply decorator |
from functools import wraps
@wraps(func) | Preserve function metadata |
Built-in Decorators
@property | Property getter |
@staticmethod | Static method (no self) |
@classmethod | Class method (cls instead of self) |
@abstractmethod | Abstract method (ABC) |
@dataclass | Auto-generate __init__, __repr__ |
@functools.cache | Memoization (Python 3.9+) |
@functools.lru_cache(maxsize=128) | LRU cache |