python命令在文件中打印文档字符串

ton*_*y19 5 python

在给定的 Python 文件中打印文档字符串的命令行是什么?

我以为有这样的事情:

$ python --showhelp foo.py

Name                 Help
--------------------------------------------------
bar                  This is the docstring for bar().
baz                  This is the docstring for baz().
Run Code Online (Sandbox Code Playgroud)

ton*_*y19 5

找到了: pydoc

对于此示例文件:

# foo.py

def bar():
  """this is the docstring for bar()"""
  print 'hello'


def baz():
  """this is the docstring for baz()"""
  print 'world'
Run Code Online (Sandbox Code Playgroud)

您可以使用以下命令将文档字符串打印到标准输出:

$ pydoc ./foo.py
Help on module foo:

NAME
    foo

FILE
    /path/to/foo.py

FUNCTIONS
    bar()
        this is the docstring for bar()

    baz()
        this is the docstring for baz()
Run Code Online (Sandbox Code Playgroud)

您还可以生成 HTML 帮助文件:

$ pydoc -w ./foo.py
wrote foo.html
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

在此处输入图片说明