Decorators¶
Legacy Wiki Page
This page was migrated from the old MoinMoin-based wiki. Information may be outdated or no longer applicable. For current documentation, see python.org.
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: |
|
builtin |
Use |
|
builtin |
Defines methods without self handle |
|
stdlib |
Retain modicum of meta infos ( |
|
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 |
|
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 |
|
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.
1 def __main__(func):
2
3 if __name__ == "__main__":
4 import sys, os
5 args = sys.argv[:]
6 args[0] = os.path.abspath(args[0])
7 func(args)