See also PythonDecoratorLibrary. And apart from decorators in the standard library, there are also lots of PyPI packages with convenient, interesting or novel use cases:
Category |
Decorator |
Summary |
builtin |
Turn function into property accessor, also: @property.setter |
|
builtin |
Use cls instead of self |
|
builtin |
Defines methods without self handle |
|
stdlib |
Retain modicum of meta infos (__doc__) of wrapped function |
|
stdlib |
Allows for divergent implementations per @f.register() |
|
stdlib/backports |
Wrap computationally expensive functions |
|
stdlib |
Transform an old-style comparison function to a key function. |
|
stdlib |
Adds any of the missing lt,le,eq,ge,gt comparison dunder methods |
|
stdlib |
Prepopulate a couple of function arguments |
|
stdlib |
Singledispatch based on function parameter or return types |
|
stdlib |
Uses generator protocol yield to turn function into with-able context mananger |
|
stdlib |
Adds various “dunder” methods to the class to streamline property access |
|
stdlib |
Run as shutdown function |
|
stdlib |
Turn into abstract base class |
|
stdlib |
Guarantee unique members in enum class |
|
stdlib |
... |
|
stdlib |
Register function as debugger callback |
|
asyncio |
Outdated scheme for async def, deprecated in 3.8 |
|
asyncio |
Reduce invocations per timeframe |
|
asyncio |
Log exceptions more instantly |
|
asyncio |
Basic benchmarking |
|
async |
make func async and execute in other process |
|
threading |
Convenient threading wrapper, and testing wrappers |
|
threading |
Aquire and release locks for functions |
|
testing |
Override function w/ signature |
|
testing |
Override function w/ signature |
|
debugging |
Capture exceptions to the log |
|
debugging |
Record invocations with arguments |
|
decorator |
Assists declaring signature-preserving and single dispatch decorators |
|
decorator |
Ease parameterizing decorators themselvess |
|
decorator |
Retain callees' function name to simplify tracebacks |
|
decorator |
Craft trivial enter/exit decorator |
|
decorator |
Simplifies decorator construnction/interactions, parameter handling |
|
decorator |
reversible decoration |
|
collect |
Argparse alternative |
|
collect |
Declarative dict structure for application settings |
|
collect |
Register function callbacks for pattern matching |
|
collect |
Plugin interface/registration |
|
typing |
Exhaustive function parameter constraints |
|
typing |
Encourages cleaner and better documented code |
|
typing |
OO-style class/method parameters |
|
typing |
decorators for str/repr, equality, immutability, and more |
|
typing |
Reduces some common boilerplate for methods |
|
typing |
.NET-like attributes |
|
parameter |
Convenient decorator-alternative to partial() |
|
parameter |
object declarations |
|
parameter |
simplifies parameter handling/assertion |
|
flow handling |
Automatically run a function if invocation script |
|
flow handling |
Reinvokes function a few times when encountering (temporary) exceptions, alt: pypi:retry-decorator |
|
flow handling |
Rerun function in case of exceptions |
|
flow handling |
method chaining (fluent interfaces) |
|
monkeypatching |
Shorthand assignment to modules/objects |
|
monkeypatching |
Swap out function in object, retain reference to original |
(Note: Not sure this is going anywhere. Relisting builtins is somewhat redundant, but on topic here. The focus is novel/interesting decorators in the wild. The categorization probably won't hold up; and probably going to split this up into sections.)
Other decorator links
PyPI decorator packages (gets interesting around page 10)
__main__
This decorator does not alter a function, but causes it to be executed if __name__ == '__main__'. This provides an experimental cleaner syntax to the traditional way of bootstrapping a python script. This should not be used on class methods.
The function gets a copied list of sys.argv arguments.