Sometimes, we need to provide parameters to a decorator. The idea is that we are going to customize the wrapping function. When we do this, decoration becomes a two-step process.
Here's a snippet showing how we provide a parameterized decorator to a function definition:
@decorator(arg) def func( ): pass
The implementation is as follows:
def func( ): pass func = decorator(arg)(func)
We've done the following three things:
- Defined a function, func
- Applied the abstract decorator to its arguments to create a concrete decorator, decorator(arg)
- Applied the concrete decorator to the defined function to create the decorated version of the function, decorator(arg)(func)
It can help to think of func = decorate(arg)(func) as having the following implementation:
concrete = decorate(arg)
func = concrete(func)
This means that a decorator with...