简体   繁体   English

Python:将foo.bar导入为bar与从foo导入导入

[英]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.

相关问题 在 python 中,“import foo.bar as bar”和“from foo import bar”有什么区别? - In python, what is the difference between 'import foo.bar as bar' and 'from foo import bar'? Google foo.bar 挑战问题:无法导入库 - Google foo.bar Challenge Issue: Can't import libraries foo.bar()和bar(foo)之间的区别? - Difference between foo.bar() and bar(foo)? 本地导入的`python -m foo.bar`与`python foo / bar.py` - `python -m foo.bar` vs. `python foo/bar.py` for local imports 如何实现“从foo.bar import baz.qux”这样的内容? - How can I achieve something like “from foo.bar import baz.qux”? 如何从python中的方法从foo导入栏执行全局操作? - How to do a global from foo import bar from a method in python? 是否可以在python中执行“ from module.foo import * as bar”? - Is it possible to do “from module.foo import * as bar” in python? 在Python中“ from bar import foo”之后,“ [foo]”有什么特殊含义吗? (也许特定于python2) - Is there some special meaning to `[foo]` right after `from bar import foo` in python? (perhaps python2 specific) isinstance(foo,bar) vs type(foo) is bar - isinstance(foo,bar) vs type(foo) is bar 使用属性装饰器后,python object 有两个非常相似的属性(foo.bar 和 foo._bar)。 那样行吗? - After using property decorator, python object has two very similar attributes (foo.bar and foo._bar). Is that ok?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM