0

Why is it possible to call a function b4 defining it or use a variable b4 creating it I.e read bottom to top in python

Python https://code.sololearn.com/c8J5KX893dPD/?ref=app

17th Jan 2020, 11:08 PM
Peter Paulinus Polycarp
Peter Paulinus Polycarp - avatar
5 Answers
+ 1
It isn't. I'm not sure what you mean by calling a function before you define it because you have literally defined your get_input() function at the very start of your code. I do understand your confusion regarding using a variable before it has been created though, but that's not what is actually happening here. Your 'verb_dict' variable is created after the definition of the function in which it is used but, crucially, it is created *before that function is called*. When the function is called, it has been created and thus can be used. Hope that makes sense.
18th Jan 2020, 3:45 PM
Russ
Russ - avatar
0
Russ thanks I get it now. The problem of calling a function i'll post it here
18th Jan 2020, 10:43 PM
Peter Paulinus Polycarp
Peter Paulinus Polycarp - avatar
0
Russ def is_even(x): if x == 0: return True else: return is_odd(x-1) def is_odd(x): return not is_even(x) print(is_odd(17)) print(is_even(23)) Thanks
18th Jan 2020, 10:45 PM
Peter Paulinus Polycarp
Peter Paulinus Polycarp - avatar
0
I'm a little unsure as to whether you're still after an answer my answer is still basically the same as above. The is_odd() function is called from the is_even() function and, although the function call is earlier in the code than the function definition, the crucial thing is that the definition comes before the is_even function is called. Consider this example: def is_even(x): if x == 0: return True else: return is_odd(x-1) print(is_even(23)) def is_odd(x): return not is_even(x) print(is_odd(17)) ...this will cause an error because the is_even() function call (which leads to the is_odd() function call) comes before the is_odd() function definition. Hope that clears things up.
19th Jan 2020, 9:50 AM
Russ
Russ - avatar
0
Thanks
20th Jan 2020, 10:32 PM
Peter Paulinus Polycarp
Peter Paulinus Polycarp - avatar