繁体   English   中英

如何在 Python 中打印模块文档

[英]How to print module documentation in Python

我知道这个问题很简单,我知道它肯定被问过很多次,我在 SO 和 Google 上都进行了搜索,但我找不到答案,可能是因为我缺乏将我所寻求的东西放入其中的能力一个恰当的句子。

我希望能够阅读我导入的文档。

例如,如果我通过“import x”导入 x,我想运行这个命令,并用 Python 或 ipython 打印它的文档。

这个命令函数是什么?

谢谢你。

附注。 我不是指 dir(),我指的是实际打印文档供我查看和阅读此模块 x 具有的功能等的函数。

您可以使用功能模块的.__doc__属性:

In [14]: import itertools

In [15]: print itertools.__doc__
Functional tools for creating and using iterators..........

In [18]: print itertools.permutations.__doc__
permutations(iterable[, r]) --> permutations object

Return successive r-length permutations of elements in the iterable.

permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)

help()__doc__在内置模块和我们自己的模块上都可以正常工作:

文件:foo.py

def myfunc():
    """
    this is some info on myfunc

    """
    foo=2
    bar=3


In [4]: help(so27.myfunc)


In [5]: import foo

In [6]: print foo.myfunc.__doc__

     this is some info on func

In [7]: help(foo.myfunc)


Help on function myfunc in module foo:

myfunc()
    this is some info on func

来自命令行的pydoc foo.bar或来自 Python 的help(foo.bar)help('foo.bar')

您可以使用help()函数来显示文档。 或者您可以选择method.__doc__描述符。

例如: help(input)将提供有关 input() 方法的文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM