14.Interactive Input Editing and History Substitution

Some versions of the Python interpreter support editing of the current inputline and history substitution, similar to facilities found in the Korn shell andthe GNU Bash shell. This is implemented using theGNU Readline library,which supports various styles of editing. This library has its owndocumentation which we won’t duplicate here.

14.1.Tab Completion and History Editing

Completion of variable and module names isautomatically enabled at interpreter startup sothat theTab key invokes the completion function; it looks atPython statement names, the current local variables, and the availablemodule names. For dotted expressions such asstring.a, it will evaluatethe expression up to the final'.' and then suggest completions fromthe attributes of the resulting object. Note that this may executeapplication-defined code if an object with a__getattr__() methodis part of the expression. The default configuration also saves yourhistory into a file named.python_history in your user directory.The history will be available again during the next interactive interpretersession.

14.2.Alternatives to the Interactive Interpreter

This facility is an enormous step forward compared to earlier versions of theinterpreter; however, some wishes are left: It would be nice if the properindentation were suggested on continuation lines (the parser knows if anINDENT token is required next). The completion mechanism mightuse the interpreter’s symbol table. A command to check (or even suggest)matching parentheses, quotes, etc., would also be useful.

One alternative enhanced interactive interpreter that has been around for quitesome time isIPython, which features tab completion, object exploration andadvanced history management. It can also be thoroughly customized and embeddedinto other applications. Another similar enhanced interactive environment isbpython.