"import *"的确切含义是什么?进口? [英] What exactly does "import *" import?
问题描述
在Python中,import *
到底要导入什么?它会导入在包含文件夹中找到的__init__.py
吗?
In Python, what exactly does import *
import? Does it import __init__.py
found in the containing folder?
例如,是否需要声明from project.model import __init__
,或者from project.model import *
是否足够?
For example, is it necessary to declare from project.model import __init__
, or is from project.model import *
sufficient?
推荐答案
from xyz import *
的优势"与其他形式的导入相反,是它可以导入一切(嗯,差不多. .[请参阅下面的(a)]所有内容),位于当前模块下的指定模块中.这样就可以使用导入的模块中的各种对象(变量,类,方法...),而无需在其前面加上模块名称.例如
The "advantage" of from xyz import *
as opposed to other forms of import is that it imports everything (well, almost... [see (a) below] everything) from the designated module under the current module. This allows using the various objects (variables, classes, methods...) from the imported module without prefixing them with the module's name. For example
>>> from math import *
>>>pi
3.141592653589793
>>>sin(pi/2)
>>>1.0
不鼓励这种做法(将*导入当前名称空间),因为
- 为命名空间冲突提供了机会(例如,如果在导入之前您有一个变量名pi) 如果导入的对象数量很大,
- 可能效率不高
- 没有明确记录变量/方法/类的起源(很高兴拥有该程序的自述文件",以便将来访问代码)
- provides the opportunity for namespace collisions (say if you had a variable name pi prior to the import)
- may be inefficient, if the number of objects imported is big
- doesn't explicitly document the origin of the variable/method/class (it is nice to have this "self documentation" of the program for future visit into the code)
因此,通常,我们将这种导入*做法限制为即席测试等.正如@Denilson-Sá-Maia指出的那样,某些库(例如pygame)具有一个子模块,其中定义了所有最常用的常量和函数,并且有效地对其进行了设计设计.用import *
导入.除了使用这些特殊的子模块外,其他情况下最好使用...:
Typically we therefore limit this import * practice to ad-hoc tests and the like. As pointed out by @Denilson-Sá-Maia, some libraries such as (e.g. pygame) have a sub-module where all the most commonly used constants and functions are defined and such sub-modules are effectively designed to be imported with import *
. Other than with these special sub-modules, it is otherwise preferable to ...:
仅明确导入一些对象
>>>from math import pi
>>>pi
>>>3.141592653589793
>>> sin(pi/2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sin' is not defined
或在其自己的名称空间下导入模块(或其别名,特别是如果它是长名称,并且程序多次引用其对象)
or import the module under its own namespace (or an alias thereof, in particular if this is a long name, and the program references its objects many times)
>>>import math
>>>math.pi
>>>3.141592653589793
etc..
>>>import math as m #bad example math being so short and standard...
>>>m.pi
>>>3.141592653589793
etc..
请参见 关于此主题的Python文档
(a)具体来说,from xyz import *
导入了什么?
如果xyz模块定义了__all__
变量,它将导入此序列中定义的所有名称,否则它将导入所有名称(除了以下划线开头的名称除外).
(a) Specifically, what gets imported with from xyz import *
?
if xyz module defines an __all__
variable, it will import all the names defined in this sequence, otherwise it will import all names, except these which start with an underscore.
注意:许多库都具有 子模块 .例如,标准库urllib
包含诸如urllib.request
,urllib.errors
,urllib.response
等子模块.常见的混淆点是
Note Many libraries have sub-modules. For example the standard library urllib
includes sub-modules like urllib.request
, urllib.errors
, urllib.response
etc. A common point of confusion is that
from urllib import *
将导入所有这些子模块. 事实并非如此:一个人需要分别用from urllib.request import *
等明确地导入这些内容.顺便说一下,这并非特定于import *
,普通的import
也不会导入子模块(但是,当然,*
通常是一切" 的简写形式,可能会误导人们认为所有子模块以及其他所有内容都将被导入).
would import all these sub-modules. That is NOT the case: one needs to explicitly imports these separately with, say, from urllib.request import *
etc. This incidentally is not specific to import *
, plain import
will not import sub-modules either (but of course, the *
which is often a shorthand for "everything" may mislead people in thinking that all sub-modules and everything else would be imported).
这篇关于"import *"的确切含义是什么?进口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!