Parameterizing a decorator
Sometimes we want to provide more elaborate 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.
When we write the following code, we provide a parameterized decorator to a function definition:
@decorator(arg) def func( ): pass
The use of the decorator is a shorthand for the following code:
def func( ): pass func= decorator(arg)(func)
Both examples do the following three things:
- Defined a function,
- Applied the abstract decorator to its arguments to create a concrete decorator,
- Applied the concrete decorator to the defined function to create the decorated version of the function,
This means that ...