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:

  1. Decorators are very applicable in Logging. See Implementation of Logging in Python Using Decorators.
  2. Decorators are extensively used in access control & authentication in Django using @login_required decorators.
  3. Decorators are used in timing function. See Execution Time Measurement in Python
  4. decorators are used in rate limiting of an API (Applicable Programming Interface).
  5. Decorators are used for Memoization while Caching. See Designing Memoization (Caching) Using Decorators.
  6. Python decorators are used in designing single-dispatch generic function.