Applications of Python Decorator
Applications from logging to authentication to rate limiting in API, Python decorators are very useful. This article explains different use case of decorators.
Decorators in Python allow us to extend the behavior of existing callable (functions, methods, and classes). Extending behavior of existing callable finds its application for:
- Decorators are very applicable in Logging. See Implementation of Logging in Python Using Decorators.
- Decorators are extensively used in access control & authentication in Django using
@login_required
decorators. - Decorators are used in timing function. See Execution Time Measurement in Python
- decorators are used in rate limiting of an API (Applicable Programming Interface).
- Decorators are used for Memoization while Caching. See Designing Memoization (Caching) Using Decorators.
- Python decorators are used in designing single-dispatch generic function.