14. Interactive Input Editing and History Substitution
14. 交互式编辑和编辑历史
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. 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.某些版本的 Python 解释器支持编辑当前输入行和编辑历史记录,类似 Kornshell 和 GNU Bash shell 的功能 。这个功能使用了 GNU Readline 来实现,一个支持多种编辑方式的库。这个库有它自己的文档,在这里我们就不重复说明了。
14.1. Tab Completion and History Editing
14.1. Tab 补全和编辑历史
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.在解释器启动的时候,补全变量和模块名的功能将 自动打开,以便在按下"Tab" 键的时候调用补全函数。它会查看 Python 语句名称,当前局部变量和可用的模块名称。处理像 "string.a" 的表达式,它会求值在最后一个 "'.'" 之前的表达式,接着根据求值结果对象的属性给出补全建议。如果拥有"__getattr__()" 方法的对象是表达式的一部分,注意这可能会执行程序定义的代码。默认配置下会把编辑历史记录保存在用户目录下名为 ".python_history"的文件。在下一次 Python 解释器会话期间,编辑历史记录仍旧可用。
14.2. Alternatives to the Interactive Interpreter
14.2. 默认交互式解释器的替代品
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.Python 解释器与早期版本的相比,向前迈进了一大步;无论怎样,还有些希望的功能:如果能在编辑连续行时建议缩进(解析器知道接下来是否需要缩进符号),那将很棒。补全机制可以使用解释器的符号表。有命令去检查(甚至建议)括号,引号以及其他符号是否匹配。
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. It can also be thoroughly customized and embedded into other applications. Another similar enhanced interactive environment is bpython.
一个可选的增强型交互式解释器是 IPython,它已经存在了有一段时间,它具有tab 补全,探索对象和高级历史记录管理功能。它还可以彻底定制并嵌入到其他应用程序中。另一个相似的增强型交互式环境是 bpython。