`from ... import` vs`import .` [英] `from ... import` vs `import .`

查看:104
本文介绍了`from ... import` vs`import .`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道来自urllib导入请求的代码片段

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` vs`import .`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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