Vivek Jha
Vivek Jha

Reputation: 257

Python function calling order

How does Python "read in" a program when you run it? For example, I don't understand why there wouldn't be a NameError: name 'cough' is not defined in the below code:

def main():
    for i in range(3):
        cough()


def cough():
    print('cough')


if __name__ == '__main__':
    main()

Basically, my question can also be stated as why do the above and below programs output the same thing:

def cough():
    print('cough')


def main():
    for i in range(3):
        cough()


if __name__ == '__main__':
    main()

Upvotes: 5

Views: 9743

Answers (5)

OLIVER.KOO
OLIVER.KOO

Reputation: 5993

Python is an interpreted language which is executed statement by statement (thanks to viraptor's tip: when compiling to bytecode it happens on whole file + per function)

In this case below the program reads line by line and knows that the function cough() and main() are defined. and later when main() is called Python knows what it is and when main() calls cough() Python knows what it is as well.

def cough():
    print('cough')


def main():
    for i in range(3):
        cough()


if __name__ == '__main__':
    main()

In this other case (below) it is the same thing. just that Python learns what main() function is before cough(). Here you might wonder: "why won't python throw an error since it doesn't know what caugh() is inside main() ? " Good question my friend.

But as long as your function is defined before you call it everything is fine. Because remember Python won't "check" if a function is defined until you call it. so in this case even tho cough() is not defined when python is reading function main() it is ok because we didn't call main() until after cough() is defined below.

def main():
    for i in range(3):
        cough()


def cough():
    print('cough')


if __name__ == '__main__':
    main()

Hope this helps you understand Python better.

Upvotes: 11

Chris
Chris

Reputation: 22953

When Python encounters a function while executing your source code, it does not immediately run the function. Rather, it compiles the function into an executable code object, and waits until you actually call the function.

This means the only time Python checks that cough() is really defined, is when you call main(). And since Python does find a cough function when main is called, it does not raise an error.

In other-words: Python does not verify the names used in function actually exist until run-time, so you so allowed to use currently undefined variable names.

This is the same reason a function such as this doesn't raise an error when defined, but it does during run-time:

>>> 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 call main before cough has been defined, you will get an error:

>>> 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 to have already been defined whether globally or locally, before you attempt to use them.

Upvotes: 1

Peter Ye
Peter Ye

Reputation: 59

That's because the real execution code is here

if name == 'main': main()

when main() is called, both main and cough have been defined

Upvotes: 0

Jos&#233; Garcia
Jos&#233; Garcia

Reputation: 136

The piece of code preventing the error to happen is this one:

if __name__ == '__main__':
    main()

because you are putting it at the end of the code, after python 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 are going to get is this:

NameError: name 'cough' is not defined

Upvotes: 2

user5597655
user5597655

Reputation:

Python reads from the top of your script to the bottom. In both examples, the cough() function is called after it has been defined.

When you defined main() with cough() inside, the cough() function is not actually run. It is not run until the last line - that is after they have already been defined.

Upvotes: 0

Related Questions