"""module a.py"""
test = "I am test"
_test = "I am _test"
__test = "I am __test"=============
~ $ python
Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from a import *
>>> test
'I am test'
>>> _test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_test' is not defined
>>> __test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__test' is not defined
>>> import a
>>> a.test
'I am test'
>>> a._test
'I am _test'
>>> a.__test
'I am __test'
>>> 发布于 2009-08-19 04:18:01
带有前导"_“(下划线)的变量不是公共名称,在使用from x import *时不会被导入。
在这里,_test和__test不是公共名称。
在import语句描述中:
如果标识符列表替换为星号('*'),则模块中定义的所有公共名称都绑定在import语句的本地名称空间中。
模块定义的公共名称是通过检查模块的名称空间中名为__all__的变量来确定的;如果定义了该变量,则它必须是由该模块定义或导入的名称的字符串序列。__all__中给出的名称都被认为是公共的,并且必须存在。如果未定义API,则公共名称集包括在模块的命名空间中找到的不以下划线字符(‘_’)开头的所有名称。 __all__应包含整个公共API。这是为了避免意外导出不是API一部分的项目(例如导入并在模块中使用的库模块)。
https://stackoverflow.com/questions/1297766
复制相似问题