一些Python模块(如math
和sys
)的pydoc文档有一个"MODULE DOCS“部分,其中包含一些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识别的变量的地方?
我在源代码中找不到它,因为在我的机器(OS X)上,math
模块是一个共享库,而sys
模块是用Python…构建的任何帮助都将不胜感激!
发布于 2010-06-24 12:43:16
在查看了pydoc
模块的代码后,我认为“module DOCS”链接只适用于标准模块,而不适用于自定义模块。
相关代码如下:
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被解释为空的"MODULE DOCS“部分。
发布于 2010-06-20 09:29:39
模块文档可能是docstring of the module。这是出现在模块顶部的纯文本(或restructured text)字符串。下面是一个例子。
"""
Module documentation.
"""
def bar():
print "HEllo"
这是针对纯Python模块的。
对于已编译的扩展模块(如math
),在初始化模块时将模块文档字符串(作为Python字符串)作为第三个参数传递给Py_InitModule3
。这将使该字符串成为模块文档字符串。您可以在数学模块here的源代码中看到这一点。
https://stackoverflow.com/questions/3078735
复制相似问题