Decorators Reference

Basic Decorator

def my_dec(func): def wrapper(*a, **kw): return func(*a, **kw) return wrapperBasic decorator pattern
@my_dec def hello(): passApply decorator
from functools import wraps @wraps(func)Preserve function metadata

Built-in Decorators

@propertyProperty getter
@staticmethodStatic method (no self)
@classmethodClass method (cls instead of self)
@abstractmethodAbstract method (ABC)
@dataclassAuto-generate __init__, __repr__
@functools.cacheMemoization (Python 3.9+)
@functools.lru_cache(maxsize=128)LRU cache