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()

7th Jan 2017, 12:00 AM
Agnelio Hlavanguane
Agnelio Hlavanguane - avatar
3 Respostas
+ 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 )
7th Jan 2017, 2:32 AM
visph
visph - avatar
+ 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)
7th Jan 2017, 12:09 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
0
Thanks. I use to see it while reading tutorial. So I was afraid something was escaping...
7th Jan 2017, 3:38 AM
Agnelio Hlavanguane
Agnelio Hlavanguane - avatar