Python function calling order

给你一囗甜甜゛ 提交于 2019-11-28 01:50:58

问题


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

回答1:


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.




回答2:


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



回答3:


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.




回答4:


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.




回答5:


That's because the real execution code is here

if name == 'main': main()

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



来源:https://stackoverflow.com/questions/45025005/python-function-calling-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!