help() 在哪里找到 Python 中的信息?
Where does help() find the info in Python?
我最近发现内置的 help()
可以打印模块、函数、方法、class 等的一些信息。但是它到底在哪里找到它显示的信息呢? Python docs 对此不要给出任何提示。
>>> import base64
>>> help(base64)
Help on module base64:
NAME
base64 - RFC 3548: Base16, Base32, Base64 Data Encodings
FILE
/usr/lib/python2.7/base64.py
..
如果你简单地做,help(help)
,你会得到
Help on _Helper in module site object:
class _Helper(__builtin__.object)
| Define the builtin 'help'.
| This is a wrapper around pydoc.help (with a twist).
|
| Methods defined here:
|
| __call__(self, *args, **kwds)
|
| __repr__(self)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
基本上,help
从 pydoc.help
获取输入。引用,pydoc
documentation,
For modules, classes, functions and methods, the displayed documentation is derived from the docstring (i.e. the __doc__
attribute) of the object, and recursively of its documentable members. If there is no docstring, pydoc
tries to obtain a description from the block of comment lines just above the definition of the class, function or method in the source file, or at the top of the module (see inspect.getcomments()
).
The built-in function help()
invokes the online help system in the interactive interpreter, which uses pydoc
to generate its documentation as text on the console.
But where exactly does it find the information it shows?
上面引用的粗体文字回答了这个问题。
它在 site.py
中,如果您使用 help.__class__
,您会看到它是 site._Helper
,它只是 pydoc.help
的包装器:
In [1]: help.__class__
Out[1]: site._Helper
_Helper
class 来自 site.py:
class _Helper(object):
"""Define the builtin 'help'.
This is a wrapper around pydoc.help (with a twist).
"""
def __repr__(self):
return "Type help() for interactive help, " \
"or help(object) for help about object."
def __call__(self, *args, **kwds):
import pydoc
return pydoc.help(*args, **kwds)
def sethelper():
__builtin__.help = _Helper()
help(object)
相当于 __builtin__.help(object)
将对象传递给 pydoc.help
.
如果创建如下结构
C (directory)
--> __init__.py (file in this directory)
然后将下面的内容写入__init__.py
'''Some help'''
和运行 help(C)
。您将看到以下内容
Help on package C:
NAME
C - Some help
FILE
/C/__init__.py
PACKAGE CONTENTS
在那种情况下 help()
从文档字符串(三个 '
之间的那个)获得帮助
我最近发现内置的 help()
可以打印模块、函数、方法、class 等的一些信息。但是它到底在哪里找到它显示的信息呢? Python docs 对此不要给出任何提示。
>>> import base64
>>> help(base64)
Help on module base64:
NAME
base64 - RFC 3548: Base16, Base32, Base64 Data Encodings
FILE
/usr/lib/python2.7/base64.py
..
如果你简单地做,help(help)
,你会得到
Help on _Helper in module site object:
class _Helper(__builtin__.object)
| Define the builtin 'help'.
| This is a wrapper around pydoc.help (with a twist).
|
| Methods defined here:
|
| __call__(self, *args, **kwds)
|
| __repr__(self)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
基本上,help
从 pydoc.help
获取输入。引用,pydoc
documentation,
For modules, classes, functions and methods, the displayed documentation is derived from the docstring (i.e. the
__doc__
attribute) of the object, and recursively of its documentable members. If there is no docstring,pydoc
tries to obtain a description from the block of comment lines just above the definition of the class, function or method in the source file, or at the top of the module (seeinspect.getcomments()
).The built-in function
help()
invokes the online help system in the interactive interpreter, which usespydoc
to generate its documentation as text on the console.
But where exactly does it find the information it shows?
上面引用的粗体文字回答了这个问题。
它在 site.py
中,如果您使用 help.__class__
,您会看到它是 site._Helper
,它只是 pydoc.help
的包装器:
In [1]: help.__class__
Out[1]: site._Helper
_Helper
class 来自 site.py:
class _Helper(object):
"""Define the builtin 'help'.
This is a wrapper around pydoc.help (with a twist).
"""
def __repr__(self):
return "Type help() for interactive help, " \
"or help(object) for help about object."
def __call__(self, *args, **kwds):
import pydoc
return pydoc.help(*args, **kwds)
def sethelper():
__builtin__.help = _Helper()
help(object)
相当于 __builtin__.help(object)
将对象传递给 pydoc.help
.
如果创建如下结构
C (directory) --> __init__.py (file in this directory)
然后将下面的内容写入__init__.py
'''Some help'''
和运行 help(C)
。您将看到以下内容
Help on package C:
NAME
C - Some help
FILE
/C/__init__.py
PACKAGE CONTENTS
在那种情况下 help()
从文档字符串(三个 '
之间的那个)获得帮助