Python 函数调用顺序
Python function calling order
如何Python"read in"一个程序当你运行呢?例如,我不明白为什么下面的代码中没有 NameError: name 'cough' is not defined
:
def main():
for i in range(3):
cough()
def cough():
print('cough')
if __name__ == '__main__':
main()
基本上,我的问题也可以表述为为什么上面和下面的程序输出相同的东西:
def cough():
print('cough')
def main():
for i in range(3):
cough()
if __name__ == '__main__':
main()
Python 从脚本的顶部读到底部。在这两个示例中,cough()
函数在定义后被调用。
当你在cough()
中定义main()
时,cough()
函数实际上并不是运行。它不是 运行 直到最后一行 - 即在它们已经被定义之后。
Python 是一种解释型语言,它是逐条执行的语句
(感谢 viraptor 的提示:当编译为字节码时,它发生在整个文件 + 每个函数上)
在这种情况下,程序逐行读取并知道函数 cough()
和 main()
已定义。稍后当调用 main()
时 Python 知道它是什么,当 main()
调用 cough()
时 Python 也知道它是什么。
def cough():
print('cough')
def main():
for i in range(3):
cough()
if __name__ == '__main__':
main()
在另一种情况下(下图)也是一样。只是 Python 在 cough()
之前学习了 main()
函数。在这里你可能想知道:“为什么 python 不会抛出错误,因为它不知道 main()
里面的 caugh()
是什么?” 很好问我朋友
但是只要你的函数在你调用它之前定义好了,一切都很好。因为记住 Python 不会 "check" 如果一个函数在你调用它之前被定义。 所以在这种情况下即使 cough()
没有定义python 正在读取函数 main()
没问题,因为我们直到下面定义了 cough()
之后才调用 main()
。
def main():
for i in range(3):
cough()
def cough():
print('cough')
if __name__ == '__main__':
main()
希望这可以帮助您更好地理解 Python。
防止错误发生的这段代码是:
if __name__ == '__main__':
main()
因为你是放在代码末尾的,所以python看完上面的所有代码。
如果你尝试写类似
def main():
for i in range(3):
cough()
if __name__ == '__main__':
main()
def cough():
print('cough')
你将得到的是:
NameError: name 'cough' is not defined
当 Python 在执行您的源代码时遇到一个函数,它不会立即 运行 该函数。相反,它将函数编译成可执行代码对象,并等待您实际调用该函数。
这意味着唯一一次 Python 检查 cough()
是否 确实 定义,是在您调用 main()
时。由于 Python 在调用 main
时确实找到了 cough
函数,因此它不会引发错误。
换句话说:Python 直到 运行 时才验证函数中使用的名称是否确实存在,因此您允许使用当前未定义的变量名称.
这与这样的函数在定义时不会引发错误的原因相同,但在 运行-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
>>>
另请注意,如果您尝试在 咳嗽定义之前调用 main
,您 将 得到一个错误:
>>> 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
>>>
这表明 Python 依赖于函数中的每个名称在您尝试使用它们之前已经在全局或本地定义。
那是因为真正的执行代码在这里
if name == 'main':
主()
调用main()时,main和cough都定义好了
如何Python"read in"一个程序当你运行呢?例如,我不明白为什么下面的代码中没有 NameError: name 'cough' is not defined
:
def main():
for i in range(3):
cough()
def cough():
print('cough')
if __name__ == '__main__':
main()
基本上,我的问题也可以表述为为什么上面和下面的程序输出相同的东西:
def cough():
print('cough')
def main():
for i in range(3):
cough()
if __name__ == '__main__':
main()
Python 从脚本的顶部读到底部。在这两个示例中,cough()
函数在定义后被调用。
当你在cough()
中定义main()
时,cough()
函数实际上并不是运行。它不是 运行 直到最后一行 - 即在它们已经被定义之后。
Python 是一种解释型语言,它是逐条执行的语句 (感谢 viraptor 的提示:当编译为字节码时,它发生在整个文件 + 每个函数上)
在这种情况下,程序逐行读取并知道函数 cough()
和 main()
已定义。稍后当调用 main()
时 Python 知道它是什么,当 main()
调用 cough()
时 Python 也知道它是什么。
def cough():
print('cough')
def main():
for i in range(3):
cough()
if __name__ == '__main__':
main()
在另一种情况下(下图)也是一样。只是 Python 在 cough()
之前学习了 main()
函数。在这里你可能想知道:“为什么 python 不会抛出错误,因为它不知道 main()
里面的 caugh()
是什么?” 很好问我朋友
但是只要你的函数在你调用它之前定义好了,一切都很好。因为记住 Python 不会 "check" 如果一个函数在你调用它之前被定义。 所以在这种情况下即使 cough()
没有定义python 正在读取函数 main()
没问题,因为我们直到下面定义了 cough()
之后才调用 main()
。
def main():
for i in range(3):
cough()
def cough():
print('cough')
if __name__ == '__main__':
main()
希望这可以帮助您更好地理解 Python。
防止错误发生的这段代码是:
if __name__ == '__main__':
main()
因为你是放在代码末尾的,所以python看完上面的所有代码。 如果你尝试写类似
def main():
for i in range(3):
cough()
if __name__ == '__main__':
main()
def cough():
print('cough')
你将得到的是:
NameError: name 'cough' is not defined
当 Python 在执行您的源代码时遇到一个函数,它不会立即 运行 该函数。相反,它将函数编译成可执行代码对象,并等待您实际调用该函数。
这意味着唯一一次 Python 检查 cough()
是否 确实 定义,是在您调用 main()
时。由于 Python 在调用 main
时确实找到了 cough
函数,因此它不会引发错误。
换句话说:Python 直到 运行 时才验证函数中使用的名称是否确实存在,因此您允许使用当前未定义的变量名称.
这与这样的函数在定义时不会引发错误的原因相同,但在 运行-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
>>>
另请注意,如果您尝试在 咳嗽定义之前调用 main
,您 将 得到一个错误:
>>> 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
>>>
这表明 Python 依赖于函数中的每个名称在您尝试使用它们之前已经在全局或本地定义。
那是因为真正的执行代码在这里
if name == 'main': 主()
调用main()时,main和cough都定义好了