`from ... import` 与 `import .` [英] `from ... import` vs `import .`
本文介绍了`from ... import` 与 `import .`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道代码片段之间是否有任何区别
I'm wondering if there's any difference between the code fragment
from urllib import request
和片段
import urllib.request
或者如果它们可以互换.如果它们是可互换的,那么这就是标准"/首选".语法(如果有的话)?
or if they are interchangeable. If they are interchangeable, which is the "standard"/"preferred" syntax (if there is one)?
推荐答案
这取决于您在引用时希望如何访问导入.
It depends on how you want to access the import when you refer to it.
from urllib import request
# access request directly.
mine = request()
import urllib.request
# used as urllib.request
mine = urllib.request()
为了简单起见或避免屏蔽内置函数,您也可以在导入时自己别名:
You can also alias things yourself when you import for simplicity or to avoid masking built ins:
from os import open as open_
# lets you use os.open without destroying the
# built in open() which returns file handles.
这篇关于`from ... import` 与 `import .`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文