0
Where is the function defined?
l think it is the second finding this situation, and os still confusing for me. in the following code, where is the function func() defined? And what it does? def decor(func): def wrap(): print("============") func() print("============") return wrap def print_text(): print("Hello world!") decorated = decor(print_text) decorated()
3 Respuestas
+ 4
func is not defined in this code. Definition of a "func" function need to look like : "def func():" ( and the next indented lines ).
The only two times where you use the identifier "func", is first declaration of an argument of the "decor()" function ( so you supposed to pass a function of any names when calling "decor(arbitrary_function)" ), and second when you want to call this argument function... Definition isn't declaration, but maybe your question is about the second and not the first?
[ EDIT ]
The "func" identifier ( but not the func() function ) is defined in the line "decorated = decor(print_text)", which pass the "print_text" value to function "decor()". In your code, if "print_text" is a correct function with a correct declaration ( "def print_text():" ) she will be executed when calling decor() function with the "decorated" identifier ( variable )
+ 1
func is defined by you ! It is only the name if the argument of the decor function.
When you wrote decor(print_text), func was print_text (only during the call)
0
Thanks. I use to see it while reading tutorial. So I was afraid something was escaping...