“从包导入*"之间的性能和“导入包" [英] Performance between "from package import *" and "import package"

查看:47
本文介绍了“从包导入*"之间的性能和“导入包"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

这篇关于“从包导入*"之间的性能和“导入包"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆