在终端中阅读python文档?

Var*_*r87 5 python python-2.7 python-3.x

有没有办法安装python文档,使其可用,就好像它是一个联机帮助页?(我知道你可以下载文件的源文件并在vim中阅读它们,使用较少或者其他什么,但我正在考虑一些不那么手动的东西.不想自己动手.)

小智 9

我不知道这是否是您想要的,但您可以在 IDLE 中执行您可以在命令行上执行的所有操作。例子:

C:>python
>>help(print())
>>help(plt.plot())
Run Code Online (Sandbox Code Playgroud)

这样您就可以访问文档


Ara*_*Fey 8

它不是文档的精确副本,但有内置help()函数.

在交互式python会话中,您只需调用help(whatever_you_want_to_read_about),例如:

>>> help(all)
Help on built-in function all in module builtins:

all(...)
    all(iterable) -> bool

    Return True if bool(x) is True for all values x in the iterable.
    If the iterable is empty, return True.
Run Code Online (Sandbox Code Playgroud)

或者,您可以启动这样的交互式帮助会话:

C:\Users\Rawing>python -c "help()"

Welcome to Python 3.4!  This is the interactive help utility.

help>
Run Code Online (Sandbox Code Playgroud)

然后只需输入您想要了解的函数/类/模块:

help> all
Help on built-in function all in module builtins:

all(...)
    all(iterable) -> bool

    Return True if bool(x) is True for all values x in the iterable.
    If the iterable is empty, return True.
Run Code Online (Sandbox Code Playgroud)


Bło*_*tek 7

在Debian(和派生的发行版,如Ubuntu)上安装pydoc包.然后你可以使用pydoc whatever命令.


Lys*_*aou 5

我不知道这是否正是您正在寻找的,但 python 交互式控制台提供了一个help命令。您可以按照以下方式使用它。

>>> help()

Welcome to Python 3.6's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/3.6/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help> list
Run Code Online (Sandbox Code Playgroud)

这将输出所有方法的完整文档list


voi*_*oid 5

您可以使用help(Class-name/method-name/anything)。但也使用__doc__

每个类和方法都附加一个特殊的__doc__ 文档字符串。例如,看看我在解释器中输入的内容。

>>> print(str.__doc__)
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.
>>> print(int.__doc__)
int(x=0) -> integer
int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is a number, return x.__int__().  For floating point
numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base.  The literal can be preceded by '+' or '-' and be surrounded
by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4
Run Code Online (Sandbox Code Playgroud)

它甚至适用于模块。

>>> import math
>>> math.__doc__
'This module is always available.  It provides access to the\nmathematical functions defined by the C standard.'
>>> math.ceil.__doc__
'ceil(x)\n\nReturn the ceiling of x as an Integral.\nThis is the smallest integer >= x.'
>>> 
Run Code Online (Sandbox Code Playgroud)

由于每个类都有一个__doc__附加的文档字符串,因此您可以使用class_name.__doc__来调用它

>>> print(ord.__doc__)
Return the Unicode code point for a one-character string.
Run Code Online (Sandbox Code Playgroud)