[英]How to print module documentation in Python
I know this question is very simple, I know it must have been asked a lot of times and I did my search on both SO and Google but I could not find the answer, probably due to my lack of ability of putting what I seek into a proper sentence.我知道这个问题很简单,我知道它肯定被问过很多次,我在 SO 和 Google 上都进行了搜索,但我找不到答案,可能是因为我缺乏将我所寻求的东西放入其中的能力一个恰当的句子。
I want to be able to read the docs of what I import.我希望能够阅读我导入的文档。
For example if I import x by "import x", I want to run this command, and have its docs printed in Python or ipython.例如,如果我通过“import x”导入 x,我想运行这个命令,并用 Python 或 ipython 打印它的文档。
What is this command-function?这个命令函数是什么?
Thank you.谢谢你。
PS.附注。 I don't mean dir(), I mean the function that will actually print the docs for me to see and read what functionalities etc. this module x has.
我不是指 dir(),我指的是实际打印文档供我查看和阅读此模块 x 具有的功能等的函数。
You can use the .__doc__
attribute of the module of function:您可以使用功能模块的
.__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)
Both of help()
and __doc__
work fine on both inbuilt and on our own modules: help()
和__doc__
在内置模块和我们自己的模块上都可以正常工作:
file: foo.py文件: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')
。
You can use help()
function to display the documentation.您可以使用
help()
函数来显示文档。 or you can choose method.__doc__
descriptor.或者您可以选择
method.__doc__
描述符。
Eg: help(input)
will give the documentation on input() method.例如:
help(input)
将提供有关 input() 方法的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.