简体   繁体   English

python是否会使用from * import *在包名称空间中添加名称?

[英]Will python add name in packages namespace with from * import *?

Assume we have package named 'package', it has a module 'module'. 假设我们有一个名为“ package”的包,它有一个模块“ module”。

If we 要是我们

from package import module 从包导入模块

Since 'package' is loaded, will package's local namespace has a name 'module'? 由于加载了“包”,包的本地名称空间是否将具有名称“模块”?

We can see a phrase like this in reference of 'import': 我们可以在引用“ import”时看到这样的短语:

The first form of import statement binds the module name in the local namespace to the module object, and then goes on to import the next identifier, if any. 导入语句的第一种形式将本地名称空间中的模块名称绑定到模块对象,然后继续导入下一个标识符(如果有)。 If the module name is followed by as, the name following as is used as the local name for the module. 如果模块名称后跟as,则名称后跟as作为模块的本地名称。

The from form does not bind the module name: it goes through the list of identifiers, looks each one of them up in the module found in step (1), and binds the name in the local namespace to the object thus found. from形式不绑定模块名称:它遍历标识符列表,在步骤(1)中找到的模块中查找每个标识符,然后将本地名称空间中的名称绑定到由此找到的对象。 As with the first form of import, an alternate local name can be supplied by specifying “as localname”. 与第一种导入形式一样,可以通过指定“ as localname”来提供备用的本地名称。 If a name is not found, ImportError is raised. 如果找不到名称,则会引发ImportError。 If the list of identifiers is replaced by a star ('*'), all public names defined in the module are bound in the local namespace of the import statement.. 如果标识符列表用星号(*)代替,则模块中定义的所有公共名称都将绑定在import语句的本地名称空间中。

What does it mean by "The from form does not bind the module name" in the second paragraph? 第二段中的“发件人形式不绑定模块名称”是什么意思?

Yes, the package.__init__ module will have access to the name module : 是的, package.__init__模块将可以访问名称module

$ mkdir package
$ touch package/__init__.py
$ touch package/module.py
$ python
Python 2.7.5 (default, Oct 28 2013, 20:45:48) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from package import module
>>> import sys
>>> sys.modules['package']
<module 'package' from 'package/__init__.py'>
>>> dir(sys.modules['package'])
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'module']
>>> sys.modules['package'].module
<module 'package.module' from 'package/module.py'>

The second paragraph you refer to talks about what names are bound by a from foo import bar import statement. 您引用的第二段讨论了from foo import bar import语句绑定的名称。 bar is bound, foo is not. bar是绑定的, foo不是。 Or, in terms of the above demonstration: 或者,就以上演示而言:

>>> module
<module 'package.module' from 'package/module.pyc'>
>>> package
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'package' is not defined

The from package import module statement added a module name to the namespace, but package was not bound. from package import module语句将module名称添加到名称空间,但未绑定package

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM