+ 1

Why is this possible?

#normally working def a(): return b() def b(): print("Hello") a() #NameError X = Y Y = 3 print(X) I think both are wrong because b() and Y are used before they're defined or assigned. But the former works normally and the latter doesn't. Why?

1st Nov 2016, 1:58 AM
Eunjae Lee
Eunjae Lee - avatar
2 Réponses
+ 2
The functions work because of how code is compiled. I wont get into the details but all you need to know is that functions aren't compiled one after another. They are basically compiled at the same time which allows your first example to run. Code in functions is compiled one after another which is why X = Y Y = 3 won't work. Don't mix them up. Functions aren't your normal lines of code.
1st Nov 2016, 6:54 AM
nedas
nedas - avatar
+ 1
Thank you very much!
1st Nov 2016, 6:58 AM
Eunjae Lee
Eunjae Lee - avatar