跳至內容

Python/Self Help

維基教科書,自由的教學讀本

這本書對於學習 Python 很有用,但書中可能沒有涵蓋某個主題。您可能想在標準庫中搜索模塊,或檢查未知對象的函數,或者您可能知道對象內部有一個必須調用的函數,但不知道其名稱。這就是交互式幫助發揮作用的地方。

內建幫助概覽

[編輯]
help() # 启动交互式帮助
help("topics") # 输出帮助主题列表
help("OPERATORS") # 显示运算符主题的帮助
help("len") # 显示 len 函数的帮助
help("re") # 显示 re 模块的帮助
help("re.sub") # 显示 re 模块的 sub 函数的帮助
help(len) # 显示传递的对象(len 函数)的帮助
help([].pop) # 显示列表的 pop 函数的帮助
dir([]) # 输出列表的属性列表,其中包括函数
import re
help(re) # 显示帮助模块的帮助
help(re.sub) # 显示 re 模块的 sub 函数的帮助
help(1) # 显示 int 类型的帮助
help([]) # 显示列表类型的帮助
help(def) # 失败:def 是一个不引用对象的关键字
help("def") # 显示函数定义的帮助

瀏覽幫助

[編輯]

要啟動 Python 的交互式幫助,請在提示符下鍵入「help()」。

>>>help()

您將看到問候語和幫助系統的簡要介紹。對於 Python 2.6,提示符將如下所示:

Welcome to Python 2.6! This is the online 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/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, or topics, type "modules",
"keywords", or "topics". Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

還要注意,提示符將從「>>>」(三個右尖括號)更改為「help>」

您只需鍵入 moduleskeywordstopics 即可訪問幫助的不同部分。

鍵入其中一個名稱將打印與相關項目相關的幫助頁面。要獲取可用模塊、關鍵字或主題的列表,請鍵入「modules」、「keywords」或「topics」。每個模塊還附帶一行摘要,說明其功能;要列出摘要中包含給定單詞(例如「垃圾郵件」)的模塊,請鍵入「modules spam」。

您可以通過鍵入「quit」或輸入空白行返回解釋器來退出幫助系統。

Help參數

[編輯]

您無需進入交互式幫助即可獲取有關特定命令的信息。

例如,只需在引號中添加字符串即可獲取有關給定主題的幫助,例如 help("object")。您還可以通過將給定對象作為參數傳遞給幫助函數來獲取有關給定對象的幫助。

外部連結

[編輯]
  • help in 2. Built-in Functions, docs.python.org