Decorators & Closures
Decorators 101
** A decorator is a callable
that takes another function as argument (the decorated func.)**
The decorator may perform some processing with the decorated function,
and return it or replaces it with another function or callable
object.
Both these code snippet shas the same effect:
1 2 3 |
|
1 2 3 4 5 |
|
- Decorators have the power to replace the decorated-function with a different one.
- Decorators are executed immediately when a module is loaded.
When does Python executs decorators?
A key feature of decorators is that they run right after the decorated function is defined.
This happens usually at the import
time (i.e. when the module is loaded).
An example
1 2 |
|