“从包导入*"之间的性能和“导入包"
from package import *
和 import package
在性能上有区别吗?
Is there any performance difference between from package import *
and import package
?
不,区别不是性能问题.在这两种情况下,都必须解析整个模块,并且将执行任何模块级代码.唯一的区别在于命名空间:首先,导入模块中的所有名称都将成为当前模块中的名称;第二种,在当前模块中只定义了包名.
No, the difference is not a question of performance. In both cases, the entire module must be parsed, and any module-level code will be executed. The only difference is in namespaces: in the first, all the names in the imported module will become names in the current module; in the second, only the package name is defined in the current module.
也就是说,很少有充分的理由使用 from foo import *
.导入模块,或从中导入特定名称.
That said, there's very rarely a good reason to use from foo import *
. Either import the module, or import specific names from it.