defining funcOne
TQ84_decorator was called for func funcOne
defining funcTwo
TQ84_decorator was called for func funcTwo
calling funcOne:
newFunc is calling funcOne
Within FuncOne
calling funcTwo:
newFunc is calling funcTwo
Within FuncTwo, p = 99
Parametrized decorator
It is possible to call a function that returns a decorator with the @function(…) syntax. Thus, it allows to create parametrized decorators.
In the following example, The function callMultiple(n) returns a decorator that calls a function n times. By using @callMultiple(5) on the fujnction F, F is executed 5 times when called:
def callMultiple(howMany):
print(f'callMultiple({howMany})')
def decorator(func):
print(f' decorator({func.__name__})')
def caller(*a, **kw):
print(' caller')
for n in range(howMany):
print(f' n = {n}')
func(*a, *kw)
return caller
print('returning decorator')
return decorator
print('Defining F')
@callMultiple(5)
def F(x):
print(f'F: x = {x}')
print('Calling F')
F('foo')