[英]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.