4.1. Decorator About

4.1.1. Rationale

  • Decorator is an object, which takes another object as it's argument

  • Since Python 2.4: PEP 318 -- Decorators for Functions and Methods

  • Since Python 3.9: PEP 614 -- Relaxing Grammar Restrictions On Decorators

  • Decorators can:

    • Do things before call

    • Do things after call

    • Modify arguments

    • Modify returned value

    • Avoid calling

    • Modify globals

    • Add or change metadata

../../_images/decorator-about-call.png

4.1.2. Syntax

  • func is a reference to function which is being decorated

  • args arbitrary number of positional arguments

  • kwargs arbitrary number of keyword arguments

  • By calling func(*args, **kwargs) you actually run original (wrapped) function with it's original arguments

>>> def mydecorator(func):
...     def wrapper(*args, **kwargs):
...         return func(*args, **kwargs)
...     return wrapper
>>>
>>>
>>> @mydecorator
... def myfunction(*args, **kwargs):
...     pass
>>>
>>>
>>> myfunction()

4.1.3. Names

>>> def mydecorator(func):
...     def wrapper(*args, **kwargs):
...         return func(*args, **kwargs)
...     return wrapper
>>> def mydecorator(fn):
...     def wrap(*a, **b):
...         return fn(*a, **b)
...     return wrap
>>> def mydecorator(fn):
...     def _(*a, **b):
...         return fn(*a, **b)
...     return _
>>> def mydecorator(fn):
...     return lambda *a, **kw: fn(*a, **kw)