我创建了两个模块,一个用于
def _func():
print "hi"
Run Code Online (Sandbox Code Playgroud)
和另一个
def func():
print "hi"
Run Code Online (Sandbox Code Playgroud)
当我在包括第一个功能的模块上使用帮助功能时,帮助模块不显示此功能.与此功能在帮助输出中显示的第二个示例相反.除了使用帮助功能之外还有其他功能吗?
Nik*_* B. 12
是的,是的(诚然细微)的区别功能明智的.假设你有一个模块A.py:
foo = 1
_bar = 2
Run Code Online (Sandbox Code Playgroud)
注意:
>>> from A import *
>>> foo
1
>>> _bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_bar' is not defined
Run Code Online (Sandbox Code Playgroud)
如果执行的import *操作,则默认行为是不导入具有前导下划线的成员.您可以通过指定__all__列表来覆盖该行为:
__all__ = ['foo', '_bar']
foo = 1
_bar = 2
Run Code Online (Sandbox Code Playgroud)
现在:
>>> from A import *
>>> (foo, _bar)
(1, 2)
Run Code Online (Sandbox Code Playgroud)
顺便说一下,__all__还会覆盖help()或显示的成员列表pydoc:
$ pydoc A | cat
...
DATA
__all__ = ['foo', '_bar']
_bar = 2
foo = 1
...
Run Code Online (Sandbox Code Playgroud)