Use cases for decorators in Python
Keyboard shortcuts
Press ← or → to navigate between chapters
Press S or / to search in the book
Press ? to show this help
Press Esc to hide this help
Use cases for decorators in Python
Core built-in decorators
- @classmethod
- @staticmethod.
- TODO @property
- TODO @getter, @setter, @deleter
Standard libraries
- TODO functools
- @cache
- @cached_property
- @lru_cache
- @total_ordering
- @singledispatch
- @wraps
- TODO dataclasses
- TODO https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager
- TODO https://docs.python.org/3/library/abc.html#abc.abstractmethod
- TODO https://docs.python.org/3/library/enum.html#enum.unique
- TODO https://docs.python.org/3/library/atexit.html#atexit.register
Some Libraries
Other uses
- Logging calls with parameters.
- Logging elapsed time of calls.
- Access control in Django or other web frameworks. (e.g. login required)
- Memoization (caching)
- Retry
- Function timeout
- Locking for thread safety
- Decorator Library