题
一些Python模块的PYDOC文档(例如 math
和 sys
)有一个“模块文档”部分,其中包含指向某些HTML文档的有用链接:
Help on module math:
NAME
math
FILE
/sw/lib/python2.6/lib-dynload/math.so
MODULE DOCS
/sw/share/doc/python26/html/math.html
如何将这样的部分包含在您自己的模块中?
更笼统地,是否有一个地方记录了PYDOC识别的变量?
我无法在源头上找到它,因为 math
模块是我的计算机(OS X)上的共享库, sys
模块是在Python建造的……任何帮助都将不胜感激!
解决方案
查看了代码之后 pydoc
模块,我认为“模块文档”链接仅适用于标准模块,而不是自定义模块。
这是相关代码:
def getdocloc(self, object):
"""Return the location of module docs or None"""
try:
file = inspect.getabsfile(object)
except TypeError:
file = '(built-in)'
docloc = os.environ.get("PYTHONDOCS",
"http://docs.python.org/library")
basedir = os.path.join(sys.exec_prefix, "lib",
"python"+sys.version[0:3])
if (isinstance(object, type(os)) and
(object.__name__ in ('errno', 'exceptions', 'gc', 'imp',
'marshal', 'posix', 'signal', 'sys',
'thread', 'zipimport') or
(file.startswith(basedir) and
not file.startswith(os.path.join(basedir, 'site-packages'))))):
if docloc.startswith("http://"):
docloc = "%s/%s" % (docloc.rstrip("/"), object.__name__)
else:
docloc = os.path.join(docloc, object.__name__ + ".html")
else:
docloc = None
return docloc
none的返回值被解释为一个空的“模块文档”部分。
其他提示
模块文档可能是 模块的docstring. 。这是纯文本(或 重组文本)字符串发生在模块顶部。这是一个例子。
"""
Module documentation.
"""
def bar():
print "HEllo"
这是针对纯Python模块的。
用于编译的扩展模块(例如 math
),您将模块DocString(作为Python字符串)作为第三参数 Py_InitModule3
当您初始化模块时。这将使字符串成为模块DocString。您可以在数学模块的来源中看到此操作 这里.
不隶属于 StackOverflow