+ 3
Decorator
Do we always need to have a function (like wrap() in our case) to decorate?
4 Réponses
+ 6
YES. Absolutely YES !. This is because a decorator function takes another function as an argument, generates a new function, augmenting the work of the original function, and returning the generated function so we can use it anywhere. The newly generated function is what is called wrapper function (yes like wrap()). Example:
def add_up(a, b):
return a + b
def decorator(func):
def wrapper():
return func * 2
return wrapper
decorated = decorator(add_up(3, 4))
print(decorated())
The above prints: 14
0
no
0
no , you do not need to ,you could return the param func directly
0
Here is an explanation of the decorator usage:
https://code.sololearn.com/cD09ijlIS6Ev/?ref=app