6.1. Decorator About¶
Since Python 2.4: PEP 318 -- Decorators for Functions and Methods
Decorator is an object, which takes another object as it's argument
Allows to do things before call
Allows to do things after call
Allows to modify arguments
Allows to modify returned value
Allows to avoid calling
Allows to modify globals
Allows to add or change metadata
6.1.1. Syntax¶
func
is a reference to function which is being decoratedargs
arbitrary number of positional argumentskwargs
arbitrary number of keyword argumentsBy 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()
6.1.2. 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)
6.1.3. Types of decorators¶
By type:
Function decorators
Class decorators
By decorated object:
Decorating function
Decorating class
Decorating method
By wrapper type:
Wrapper function
Wrapper class
Wrapper method
By number of arguments:
Without arguments
With arguments
6.1.4. Decorator Types¶
Function decorators
Class decorators
In this example:
obj
is a decorated objectdoesn't matter, whether is a function, class or method
>>> def mydecorator(obj):
... ...
>>> class MyDecorator:
... def __init__(self, obj):
... ...
6.1.5. Wrapper Type¶
Wrapper function
Wrapper class
Wrapper method
In this example:
obj
is a decorated objectdoesn't matter, whether is a function, class or method
If
obj
andWrapper
are classes,Wrapper
can inherit fromobj
(to extend it)
>>> def mydecorator(obj):
... def wrapper(*args, **kwargs):
... ...
... return wrapper
>>> def mydecorator(obj):
... class Wrapper:
... def __init__(self, *args, **kwargs):
... ...
... return Wrapper
>>> class MyDecorator:
... def __init__(self, obj):
... ...
...
... def __call__(self, *args, **kwargs):
... ...
6.1.6. Decorated Object¶
Decorating function (by convention
func
orfn
)Decorating class (by convention
cls
)Decorating method (by convention
mth
,meth
ormethod
)
>>> def mydecorator(func):
... ...
>>> def mydecorator(cls):
... ...
>>> def mydecorator(mth):
... ...
>>> class MyDecorator:
... def __init__(self, func):
... ...
>>> class MyDecorator:
... def __init__(self, cls):
... ...
>>> class MyDecorator:
... def __init__(self, mth):
... ...
6.1.7. Usage¶
>>> @mydecorator
... def myfunction(*args, **kwargs):
... ...
>>> class MyClass:
... @mydecorator
... def mymethod(self, *args, **kwargs):
... ...
>>> @mydecorator
... class MyClass:
... ...
>>> @MyDecorator
... def myfunction(*args, **kwargs):
... ...
>>> class MyClass:
... @MyDecorator
... def mymethod(self, *args, **kwargs):
... ...
>>> @MyDecorator
... class MyClass:
... ...
6.1.8. Arguments¶
Without arguments
With arguments
>>> @mydecorator
... def myfunction(*args, **kwargs):
... ...
>>> @MyDecorator
... def myfunction(*args, **kwargs):
... ...
>>> @mydecorator('arg1', 'arg2')
... def myfunction(*args, **kwargs):
... ...
>>> @MyClass('arg1', 'arg2')
... def myfunction(*args, **kwargs):
... ...