如何使用 pydoc 只查看文档文本

How to view only doc text with pydoc

作为一名 perl 开发人员,我知道 perldoc 实用程序。我是 python 的新手,我在寻找相同的东西并找到了 pydoc。 但是 pydoc 的问题是它 也在执行我的脚本 而我只想看到写在三重引号内的文档。 任何帮助将不胜感激。

最好的方法可能是在 Python 中工作(而不是命令行)。我们以模块 sys 为例。首先导入你的模块,然后使用 help(),像这样:

import sys
help(sys)

如果您想查看文档字符串:

print(sys.__doc__)

对于自己开发的模块,可以在documentation:

中描述,内置一个守卫,使模块不被执行。

Note: In order to find objects and their documentation, pydoc imports the module(s) to be documented. Therefore, any code on module level will be executed on that occasion. Use an if __name__ == '__main__': guard to only execute code when a file is invoked as a script and not just imported.