python--特殊变量

1. __all__

python--特殊变量_第1张图片
Paste_Image.png

from module import *, 默认导出不以下划线开头的所有成员, 但是加了__all__, 导出列表中的所有。

In [1]: from hello import *

In [2]: _name
Out[2]: 'xiaoming'

In [3]: test_hello()
hello

In [4]: age
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
 in ()
----> 1 age

NameError: name 'age' is not defined

In [5]: _test_name()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
 in ()
----> 1 _test_name()

NameError: name '_test_name' is not defined

注意
__all__ 只影响到了 from module import * 这种导入方式,对于 from module import member 或者 import module 导入方式并没有影响,仍然可以从外部导入.

In [6]: from hello import age

In [7]: age
Out[7]: 18

In [8]: import hello

In [9]: hello.age
Out[9]: 18

你可能感兴趣的:(python--特殊变量)