+ 5

I need help with decorators in Python.

https://code.sololearn.com/cK7xx5ydzKf7/?ref=app In this code I can't understand these 2 things: 1. The job of the last line of this code; 2. And the last line of the decor funtion, I mean why that wrap doesn't have a pair of braces just after it like this wrap()?

12th Apr 2019, 2:01 PM
Abdul S Ansari
Abdul S Ansari - avatar
3 Respuestas
+ 6
Last line is calling the decorated function decorated() Last line of decorated function return wrap You are returning the function, not calling it. The decorated function is kept in variable called "decorated" and it could be called later with decorated()
12th Apr 2019, 2:53 PM
Javier Felipe Toribio
Javier Felipe Toribio - avatar
+ 2
1 - decorated is another name for the function decor(print_text), and it is also a decoration due to the wrap(), so to actually use this function you must call it through decorated or use the decorator @decor above a function. Thus decorated(). (Because it returns a function) 2 - it is because you want it to return a function, not run it. If you would return wrap(), it wouldn't run firstly because func is not defined in that scope.
12th Apr 2019, 2:57 PM
Luis Sepúlveda
Luis Sepúlveda - avatar
0
I think that this code could help https://code.sololearn.com/cXiJWAB6AhS9/?ref=app
25th May 2019, 2:23 PM
simone
simone - avatar