我有一个非常简单的例子:
#!/usr/bin/env python
#a() # 1: NameError: name 'a' is not defined
#b() # 1: NameError: name 'b' is not defined
#c() # 1: NameError: name 'c' is not defined
def a():
c() # note the forward use here...
#a() #2: NameError: global name 'c' is not defined
#b() #2: NameError: name 'b' is not defined
#c() #2: NameError: name 'c' is not defined
def b():
a()
#a() #3: NameError: global name 'c' is not defined
#b() #3: NameError: global name 'c' is not defined
#c() #3: NameError: name 'c' is not defined
def c():
pass
a() # these all work OK...
b()
c()
我在 Python 源文件中按字母顺序定义了 3 个名为 a()
、b()
和 c()
的函数。每个函数定义的主体都是对其他函数之一的调用。您可以从我的评论中看到,我必须在它们的定义下方(在文本文件中)对这些函数中的第一个进行初始调用,但您不一定需要在另一个调用它的函数之上定义一个函数。
当然,在所有函数定义(在 Python 和许多其他语言中)下面放置第一个可执行代码似乎是一种常见的做法,现在我明白为什么了。在 C 和 C++ 中,头文件负责这一点。在 Pascal 中,您必须在使用之前定义名称。
假设,例如,你在 Python 中有这个:
def a(a_arg): c(a_arg)
def b(b_arg): a()
def c(a_arg,b_arg): b(b_arg)
a(1)
它会在运行时正确地失败 TypeError: c() 需要 2 个参数(1 个给定)
,而其他错误是编译时。 (在 C 中,这会编译然后神秘地失败......)
在 Perl 中,由于子例程名称通常在运行时解析,您可以按任意顺序使用 Perl 定义和代码:
#!/usr/bin/env perl
a();
b();
c();
sub a{ c(); }
sub b{ a(); }
sub c{ return; }
在 C 中,使用未经原型(prototype)化且不应被忽略的函数是错误或警告(取决于实现)。
你可以有这个:
void a(void) { c(); } /* implicitly assumed to be int c(...) unless prototyped */
void b(void) { a(); }
void c(void) { return; }
int main(void) {
a();
return EXIT_SUCCESS;
}
我的假设和困惑是:如果 Python 直到运行时才解析子例程名称,为什么源代码编译阶段会失败,因为前向声明尚未定义的子例程名称?它是否记录在某处(除了通过观察其他代码)您不能在子例程定义之上的源文件中包含代码?
Python 似乎有 dynamic name resolution 的元素(在 a()
中使用 c()
在源文件下面定义之前)和静态名称解析的元素(Python 运行调用失败到 a()
如果放在源文件中它的定义之上。)
是否有 Python 版本的 THIS DOCUMENT涵盖 Perl 可执行文件的生命周期以及如何在源文件解释和运行时解析名称?
在 Python 脚本的定义顺序上是否有明确的描述,说明函数可以具有其他子例程名称的前向定义,但主代码不能?
编辑和结论
在一些激烈的评论和我的一些研究之后,我得出结论,我的问题实际上更多是关于如何解析名称,以及如何在 Python 中定义命名空间、范围和模块。
来自 carot-top :
"a callable must be defined before it is called in the current namespace." and this link on scopes and names
来自 S.Lott :
"When a name is used in a code block, it is resolved using the nearest enclosing scope." and this link to the execution life of a Python script.
来自 Python 文档:
"A scope defines the visibility of a name within a block." From the Python Execution model
"A module can contain executable statements as well as function definitions." in more about modules
"In fact function definitions are also ‘statements’ that are ‘executed’; the execution of a module-level function enters the function name in the module’s global symbol table." in the footnote thereto.
我自己的认识(呃!):
每个 Python 源文件都被 Python 视为“模块”:“模块是包含 Python 定义和语句的文件。”
与 Perl(我有更多经验)不同,Python 在读取模块时执行它们。因此,引用尚未在同一模块中定义的函数的立即可执行语句失败。
最佳答案
定义的顺序只是“在调用之前必须定义所有内容”。差不多就这些了。</p>
编辑(在评论中包含答案,已阐明):
类似的原因
def call_a():
a()
def a():
pass
call_a()
在 a
甚至被定义为函数之前在 call_a()
中有 a()
时工作是因为 Python 实际上只根据需要查找符号的值。当评估 call_a
时,a()
调用基本上存储为字节码指令,以“查找 a
是什么并调用它”时机成熟时,直到您真正调用底部的 call_a()
。
这是 call_a
的反汇编字节码的样子(通过 dis.dis
):
Disassembly of call_a:
2 0 LOAD_GLOBAL 0 (a)
3 CALL_FUNCTION 0
6 POP_TOP
7 LOAD_CONST 0 (None)
10 RETURN_VALUE
所以基本上,当你点击 call_a
时,它会将存储为 a
的所有内容加载到堆栈中,将其作为函数调用,然后在之前弹出返回值返回 None
,这对于任何未显式返回的内容都会隐式发生(call_a() is None
返回 True
)
关于Python:名称解析;函数def的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4937532/