[英]Python: import foo.bar as bar vs from foo import bar
Given that foo.bar
is a module, what is the difference between 假设
foo.bar
是一个模块,那么之间有什么区别
import foo.bar as bar
and 和
from foo import bar
I'm particularly interested in what lazy importing of modules does to this. 我对模块的延迟导入对此有什么特别感兴趣。
Note: This is not a duplicate of this question . 注意:这不是此问题的重复项。
In the first code line: 在第一行代码中:
import foo.bar as bar
Here you are importing bar
from foo
package but why added as bar
means when you need to access any function func
in that bar
. 在这里,您是从
foo
包中导入bar
,但为什么将其添加as bar
意味着何时需要访问该bar
任何函数func
。 You have to access like foo.bar.func
but when you added as bar
you just use 您必须像
foo.bar.func
这样访问,但是当您添加as bar
您只需要使用
bar.func
The same to the line from foo import bar
just importing the bar
与
from foo import bar
的行相同,只是要导入bar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.