Python function call order
How does Python "read" a program when it runs? For example, I don't understand why the code below will not NameError: name 'cough' is not defined
:
def main():
for i in range(3):
cough()
def cough():
print('cough')
if __name__ == '__main__':
main()
Basically, my question can also be phrased like this: why does the above and the below program produce the same thing:
def cough():
print('cough')
def main():
for i in range(3):
cough()
if __name__ == '__main__':
main()
source to share
Python is an interpreted language that is a statement executed by a statement (thanks to the viraptor hint: when compiled to bytecode, this happens for the whole file + per function)
In this case, the program below reads line by line and knows that the functions cough()
and are defined main()
. and later when main()
called Python it knows what it is and when it main()
calls cough()
Python it knows what it is.
def cough():
print('cough')
def main():
for i in range(3):
cough()
if __name__ == '__main__':
main()
In this other case (below) they are the same thing. it's just that Python knows what the function main()
is before cough()
. At this point, you might be wondering, "Why doesn't python throw an error because it doesn't know what caugh()
's inside main()
?" Good question my friend.
But as long as your function is defined before you call it, you're fine. Because remember, Python won't "check" if a function is defined until you name it. so in this case even tho is cough()
not defined when the python read function main()
is ok, because we didn't call main()
until cough()
below defined.
def main():
for i in range(3):
cough()
def cough():
print('cough')
if __name__ == '__main__':
main()
Hope this helps you understand Python better.
source to share
The piece of code to prevent the error is as follows:
if __name__ == '__main__':
main()
because you put it at the end of the code after python has read all the code above. If you try to write something like
def main():
for i in range(3):
cough()
if __name__ == '__main__':
main()
def cough():
print('cough')
All you get is this:
NameError: name 'cough' is not defined
source to share
When Python encounters a function while executing source code, it doesn't run the function right away. Rather, it compiles that function into an executable code object and waits for you to actually call the function.
This means that only Python checks what is cough()
actually defined when you call main()
. And because Python finds the function cough
when called main
, it doesn't throw an error.
In other words: Python does not check that the names used in the function actually exist before runtime, so you are allowed to use undefined variable names at present .
For the same reason, a function like this does not throw an error when it is defined, but it is executed at runtime:
>>> def func():
a + b
>>> func # func was compiled...
<function func at 0x7f8ddd5d6488>
>>> func() # but we cannot call it.
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
func() # but we cannot call it.
File "<pyshell#7>", line 2, in func
a + b
NameError: name 'a' is not defined
>>>
Also note that if you try to trigger main
before the cough has been identified, you will receive an error message:
>>> def main():
for i in range(3):
cough()
>>> main()
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
main()
File "<pyshell#12>", line 3, in main
cough()
NameError: name 'cough' is not defined
>>>
This shows that Python relies on every name in your function that has already been defined both globally and locally before trying to use them.
source to share
Python reads from the top of your script from the bottom. In both examples, the function cough()
is called after it has been defined.
When you define main()
with cough()
inside, the function cough()
is not actually fired. It doesn't run until the last line - that is, after they've already been defined.
source to share