问题描述
如果你碰巧有
from <module> import *
在你的程序(或模块)中间,你会收到警告:
in the middle of your program (or module), you would get the warning:
/tmp/foo:100: SyntaxWarning: import * only allowed at module level
我理解为什么通常不鼓励 import *
(命名空间不可见),但是在很多情况下它会证明很方便,尤其是在代码不会与任何人共享.
I understand why import *
is discouraged in general (namespace invisibility),but there are many situations where it would prove convenient, especially wherecode is not shared with anyone.
那么,谁能详细解释一下为什么 from 导入 *
应该在所有可能的情况下都被禁止?
So, can anyone explain precisely in detail why from <module> import *
shouldbe prohibited in all possible cases?
推荐答案
我相信在你的程序中间"你在谈论一个导入inside函数定义:
I believe by "in the middle of your program" you are talking about an import inside a function definition:
def f():
from module import * # not allowed
这是不允许的,因为它会使优化函数体变得太难.Python 实现希望在对函数进行字节编译时知道函数局部变量的所有名称,以便它可以将变量引用优化为 (CPython) 虚拟机的操作数堆栈上的操作,或者至少是局部变量槽操作而不是在外部命名空间中查找.如果您可以将模块的全部内容转储到函数的本地命名空间中,那么编译器将不得不假设函数中的任何名称都可能引用模块全局,因为名称列表带来了in by from module import *
只在运行时知道.
This is not allowed because it would make optimizing the body of the function too hard. The Python implementation wants to know all of the names of function-local variables when it byte-compiles a function, so that it can optimize variable references into operations on the (CPython) virtual machine's operand stack, or at least to local variable-slot operations rather than lookups in outer namespaces. If you could dump the entire contents of a module into a function's local namespace, then the compiler would have to assume that any name in the function might possibly refer to a module global, because the list of names brought in by from module import *
is only known at runtime.
将 from module import *
放在 顶级声明之间是一种糟糕的风格,但这是允许的:
Putting from module import *
in between top-level declarations is poor style, but it's allowed:
def f():
...
from module import *
def g():
...
编辑 2013 年 4 月:在研究其他内容时,我发现 Python 2.1 中引入了此限制,这是 嵌套作用域"功能 (PEP 227).引用链接:
EDIT April 2013: While looking into something else, I discovered that this restriction was introduced in Python 2.1, as a consequence of the "Nested Scopes" feature (PEP 227). Quoting from the link:
更改的一个副作用是 from module import *
和 exec
语句在某些条件下在函数作用域内是非法的.Python 参考手册一直说 from module import *
只在模块的顶层是合法的,但 CPython 解释器以前从未强制执行过.作为嵌套作用域实现的一部分,将 Python 源代码转换为字节码的编译器必须生成不同的代码来访问包含作用域中的变量.from module import *
和 exec
使编译器无法弄清楚这一点,因为它们向本地命名空间添加了在编译时不可知的名称.因此,如果函数包含函数定义或带有自由变量的 lambda
表达式,编译器将通过引发 SyntaxError
异常来标记它.
这阐明了注释中讨论的 Python 3.x 与 2.x 行为.它总是与语言规范相反,但 CPython 2.1 到 2.7 仅在函数内为 from module import *
发出错误,如果它可能影响编译器知道变量是本地绑定还是在一个包含范围.在 3.x 中,它已提升为无条件错误.
This clarifies the Python 3.x vs 2.x behavior discussed in the comments. It is always contrary to the language specification, but CPython 2.1 through 2.7 only issue an error for from module import *
within a function if it might affect the compiler's ability to know whether a variable binds locally or in a containing scope. In 3.x it has been promoted to an unconditional error.
编辑之子: ...显然 flashk 几年前在另一个答案中指出了这一点,引用了Python 2.1 中的新功能"的同一段.你们现在就去投票吧.
SON OF ... and apparently flashk pointed this out years ago in another answer, quoting the same paragraph of "What's New in Python 2.1" yet. Y'all go upvote that now.
这篇关于Python:为什么应该“来自<module>进口*'被禁止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!