Cited, real implementations to study (or point an agent at) when designing or reviewing decorator-shaped code.
functools.lru_cache/functools.cache. Memoization as a decorator — wrap a function, gain a cache andcache_info()statistics. The pattern shipping in the box. docs.python.org/3/library/functools.html#functools.lru_cachefunctools.wraps. A decorator whose only job is making other decorators honest — it copies the wrapped function's identity onto the wrapper. docs.python.org/3/library/functools.html#functools.wrapscontextlib.contextmanager. Wraps a generator into a context manager — a decorator that changes the kind of the thing it wraps. docs.python.org/3/library/contextlib.html#contextlib.contextmanager
- Flask routing.
@app.route("/path")registers view functions into the URL map at definition site — decorator as registration API. flask.palletsprojects.com/en/stable/quickstart/#routing - Django's
@login_required. Access control layered onto views without touching them. docs.djangoproject.com/en/stable/topics/auth/default/#the-login-required-decorator tenacity. Production retry policies (backoff, jitter, stop conditions) stacked onto callables — this unit'sretrygrown up. tenacity.readthedocs.ioclick. Whole CLIs built by stacking@click.commandand@click.option— decorators composing a program's surface. click.palletsprojects.com
Every one preserves the wrapped callable's contract (arguments in, result
out) and adds exactly one concern beside it. And every serious one calls
functools.wraps — check for it first when reviewing any hand-rolled
decorator.