簡體   English   中英

在終端中閱讀 python 文檔?

[英]Reading python documentation in the terminal?

有沒有辦法安裝 python 文檔,使它像聯機幫助頁一樣可用? (我知道您可以下載文檔的源文件並在 vim 中閱讀它們,使用更少或其他任何東西,但我正在考慮一些更少手動的東西。不想自己動手。)

它不是文檔的精確副本,但有內置的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.

或者,您可以像這樣啟動交互式幫助會話:

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

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

help>

然后只需鍵入您想了解的函數/類/模塊:

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.

在 Debian(和衍生發行版,如 Ubuntu)上安裝pydoc包。 然后你可以使用pydoc whatever命令。

如果這是您想要的,我不知道,但是您可以在 IDLE 中做的所有事情都可以在命令行上做。 例子:

C:>python
>>help(print())
>>help(plt.plot())

這樣您就可以訪問文檔

我不知道這是否正是您正在尋找的,但 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

這將輸出所有list方法的整個文檔。

您可以使用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

它甚至適用於模塊。

>>> 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.'
>>> 

由於每個類都有一個__doc__ ,它是一個附加的文檔字符串,您可以使用class_name.__doc__調用它

>>> print(ord.__doc__)
Return the Unicode code point for a one-character string.

您可以使用BIF help()

在終端中轉到 Python REPL

python

現在輸入幫助()

現在,例如,如果您希望獲得ipaddress 包中的 IPv4Network類的幫助,那么您只需指定 IPv4Network 的完全限定路徑,即ipaddress.IPv4Network

例子:

$ python3
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help()
Welcome to Python 3.6's help utility!
help> ipaddress.IPv4Network
Help on class IPv4Network in ipaddress:

ipaddress.IPv4Network = class IPv4Network(_BaseV4, _BaseNetwork)
 |  This class represents and manipulates 32-bit IPv4 network + addresses..
 |  
 |  Attributes: [examples for IPv4Network('192.0.2.0/27')]
 |      .network_address: IPv4Address('192.0.2.0')
 |      .hostmask: IPv4Address('0.0.0.31')
 |      .broadcast_address: IPv4Address('192.0.2.32')
 |      .netmask: IPv4Address('255.255.255.224')
 |      .prefixlen: 27
 |  
 |  Method resolution order:
 |      IPv4Network
 |      _BaseV4
 |      _BaseNetwork
 |      _IPAddressBase
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __init__(self, address, strict=True)
 |      Instantiate a new IPv4 network object.
 |      
 |      Args:
 |          address: A string or integer representing the IP [& network].
 |            '192.0.2.0/24'
 |            '192.0.2.0/255.255.255.0'
 |            '192.0.0.2/0.0.0.255'
 |            are all functionally the same in IPv4. Similarly,
 |            '192.0.2.1'

等等

我會回答,因為我對接受的答案不滿意。 可能是因為我不使用 IDLE。

請注意,我使用的是 Ubuntu(終端)。 可能在其他操作系統中,它的工作原理相同。

有沒有辦法安裝它? 沒必要。 我發現它是默認出現的。 如何訪問它? 在 python 的 shell 中使用help()命令。

  1. 在 shell 中,鍵入命令help()
  2. 現在您在幫助實用程序中,輸入您想要閱讀其文檔的任何內容。 q退出文檔並鍵入quit退出幫助實用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM