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

  • classmethod

  • staticmethod

  • pytest

  • Common decorators are @classmethod and @staticmethod.

  • Flask uses them to mark and configure the routes.

  • Pytest uses them to add marks to the tests.

  • functools

  • dataclasses

  • 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