Run the tests with:
make test
Function decorators
1.1
decorator_function_1.py.1.2
decorator_function_2.pyClass decorators
2.1
decorator_class_1.py2.2
decorator_class_2.py2.3
decorator_class_3.pyOther decorators (generators, coroutines, etc.).
Passing Arguments to Decorators
4.1 As a decorator function:
decorator_parametrized_1.py4.2 As a decorator object:
decorator_parametrized_2.py
Keep the properties of the original attributes (docstring, name, etc.), by using
functools.wraps.1.1
decorator_wraps_1.pyDon't have side effects on the main body of the decorator. This will run at parsing time, and will most likely fail.
2.1
decorator_side_effects_1.py2.2
decorator_side_effects_2.pyMake sure the decorated function is equivalent to the wrapped one, in terms of inspection, signature checking, etc.
3.1 Create decorators that work for functions, methods, static methods, class methods, etc.
3.2 Use the
wraptpackage to create effective decorators.
The DRY Principle with Decorators (reusing code).
Separation of Concerns with Decorators.
Listings:
decorator_SoC_{1,2}.py