Python 程式設計/自學
外觀
本書對學習 Python 很有用,但書中可能沒有涵蓋某個主題。你可能想在標準庫中搜索模組,或者檢查未知物件的函式,或者你可能知道你必須在物件內部呼叫某個函式,但你不知道它的名稱。這就是互動式幫助發揮作用的地方。
內建互動式幫助一覽
help() # Starts an interactive help
help("topics") # Outputs the list of help topics
help("OPERATORS") # Shows help on the topic of operators
help("len") # Shows help on len function
help("re") # Shows help on re module
help("re.sub") # Shows help on sub function from re module
help(len) # Shows help on the object passed, the len function
help([].pop) # Shows help on the pop function of a list
dir([]) # Outputs a list of attributes of a list, which includes functions
import re
help(re) # Shows help on the help module
help(re.sub) # Shows help on the sub function of re module
help(1) # Shows help on int type
help([]) # Shows help on list type
help(def) # Fails: def is a keyword that does not refer to an object
help("def") # Shows help on function definitions
要啟動 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 https://docs.python.club.tw/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>" 你可以透過鍵入以下內容訪問幫助的不同部分模組, 關鍵字,或者主題.
鍵入其中之一的名稱將列印與該專案相關的幫助頁面。要獲得可用模組、關鍵字或主題的列表,請鍵入 "modules"、"keywords" 或 "topics"。每個模組都附帶一個關於其功能的單行摘要;要列出摘要包含給定單詞(如 "spam")的模組,請鍵入 "modules spam"。
你可以透過鍵入 "quit" 或輸入空行退出幫助系統,以返回到直譯器。
你可以無需進入互動式幫助即可獲取有關特定命令的資訊。
例如,你可以透過新增用引號括起來的字串來獲取有關給定主題的幫助,例如help("object")。你也可以透過將它作為引數傳遞給 help 函式來獲取有關給定物件的幫助。
- 幫助 在 2. 內建函式中,docs.python.org