14. Interactive Input Editing and History Substitution交互式输入编辑和历史替换

Some versions of the Python interpreter support editing of the current input line and history substitution, similar to facilities found in the Korn shell and the GNU Bash shell. Python解释器的一些版本支持编辑当前输入行和历史替换,类似于Korn shell和GNU Bash shell中的功能。This is implemented using the GNU Readline library, which supports various styles of editing. This library has its own documentation which we won’t duplicate here.这是使用GNU Readline库实现的,该库支持各种编辑样式。这个库有自己的文档,我们在这里不复制。

14.1. Tab Completion and 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. 变量和模块名的完成在解释器启动时自动启用,以便Tab键调用完成功能;它查看Python语句名、当前的局部变量和可用的模块名。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. 对于string.a这样的虚线表达式,它将计算表达式直到最后的'.'然后根据结果对象的属性建议补全。Note that this may execute application-defined code if an object with a __getattr__() method is part of the expression. 请注意,如果带有__getattr__()方法的对象是表达式的一部分,则这可能会执行应用程序定义的代码。The default configuration also saves your history into a file named .python_history in your user directory. 默认配置还将历史保存到用户目录中名为.python_history的文件中。The history will be available again during the next interactive interpreter session.历史记录将在下一次交互式解释器会议期间再次提供。

14.2. Alternatives to the Interactive Interpreter交互式解释器的替代方案

This facility is an enormous step forward compared to earlier versions of the interpreter; however, some wishes are left: It would be nice if the proper indentation were suggested on continuation lines (the parser knows if an indent token is required next). 与早期版本的解释器相比,这种设备是一个巨大的进步;然而,还有一些愿望:如果在继续行上建议适当的缩进,那就太好了(解析器知道接下来是否需要缩进标记)。The completion mechanism might use 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 quite some time is IPython, which features tab completion, object exploration and advanced history management. IPython是一个已经存在了相当长一段时间的增强型交互式解释器,它具有标签完成、对象探索和高级历史管理功能。It can also be thoroughly customized and embedded into other applications. 它还可以完全定制并嵌入到其他应用程序中。Another similar enhanced interactive environment is bpython.另一个类似的增强交互环境是bpython