14. 互動式輸入編輯和歷史記錄替換

有些版本的 Python 直譯器支援當前輸入內容的編輯和歷史記錄的替換 (history substitution),類似在 Korn shell 和 GNU Bash shell 中的功能。這個功能是用 GNU Readline 函式庫來實作,它支援多種編輯的風格。這個函式庫有它自己的說明文件,在這裡我們就不重複了。

14.1. Tab 鍵自動完成 (Tab Completion) 和歷史記錄編輯 (History Editing)

Completion of variable and module names is automatically enabled at interpreter startup so that the Tab key invokes the completion function; it looks at Python statement names, the current local variables, and the available module names. For dotted expressions such as string.a, it will evaluate the expression up to the final '.' and then suggest completions from the attributes of the resulting object. Note that this may execute application-defined code if an object with a __getattr__() method is part of the expression. The default configuration also saves your history into a file named .python_history in your user directory. The history will be available again during the next interactive interpreter session.

14.2. 互動式直譯器的替代方案

與早期版本的直譯器相比,上述功能的出現的確是一個巨大的進步;但還是有一些願望沒有被實現:如果能在每次換行時給予適當的縮排建議(剖析器 (parser) 知道下一行是否需要縮排標記 (indent token)),那就更棒了。自動完成機制可能會使用直譯器的符號表。若有一個命令能夠檢查(或甚至建議)括號、引號和其他符號的匹配,那也會很有用。

有一個功能增強的互動式直譯器替代方案,已經存在一段時間,稱為 IPython,它具有 Tab 鍵自動完成、物件探索和進階歷史記錄管理等特色。它也可以完全客製化並被嵌入到其他應用程式中。另一個類似的增強型互動式環境,稱為 bpython