39

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

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

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

这个命令功能是什么?

谢谢你。

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

4

3 回答 3

29

您可以使用.__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
于 2012-10-24T18:00:01.293 回答
21

pydoc foo.bar从命令行help(foo.bar)help('foo.bar')Python。

于 2012-10-24T17:57:33.207 回答
1

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

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

于 2019-02-27T18:05:03.040 回答