Advanced Python Decorator Topics

PyPI: functools

All Python Decorators

Python Class Decorators


Built-In Decorators

@staticmethod

@classmethod

@lru_cache

@property

Built-In Abstract Decorators

@abstractstaticmethod

@abstractclassmethod

@abstractmethod

@abstractproperty

*ASP Decorators*

@Aspect

@weave


Decorator Inputs

Decorations for Logging

Real Life Example

Static Method

Logging Usage of Functions

Timing Function Calls

Class Method



When to use Decorators

  1. Testing performance
  2. Perform caching
  3. Verify permissions
  4. Run the same code on multiple functions
  5. Check arguments type at runtime
  6. Benchmark function calls
  7. Check metadata (permissions, roles, etc.)
  8. Metaprogramming

Ideas