+ 3

Decorator

Do we always need to have a function (like wrap() in our case) to decorate?

19th Jul 2016, 1:42 PM
Ankit Gangwal
Ankit Gangwal - avatar
4 ответов
+ 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
19th Jul 2016, 6:00 PM
Benneth Yankey
Benneth Yankey - avatar
0
no
19th Jul 2016, 2:10 PM
sanketpal
0
no , you do not need to ,you could return the param func directly
9th Jan 2017, 5:18 AM
nate
nate - avatar
0
Here is an explanation of the decorator usage: https://code.sololearn.com/cD09ijlIS6Ev/?ref=app
7th Jun 2018, 2:02 AM
Denys Yeromenko
Denys Yeromenko - avatar