Python 官方简明教程 13


跳转至: 导航, 搜索

-13. 交互式输入编辑和历史演化 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. This is implemented using the GNU Readline library, which supports Emacs-style and vi-style editing. This library has its own documentation which I won’t duplicate here; however, the basics are easily explained. The interactive editing and history described here are optionally available in the Unix and Cygwin versions of the interpreter.

Python解释器当前输入支持编辑某些版本替代路线和历史,类似于在Korn shell中发现的设施和 GNU的bash外壳。这是通过使用`_`的GNU Readline的图书馆,支持Emacs风格和vi风格编辑。这个库有自己的文件,我不会在这里重复,但很容易的基础知识解释。交互式编辑和这里所描述的历史选择可在翻译和Cygwin版本的Unix。

This chapter does not document the editing facilities of Mark Hammond’s PythonWin package or the Tk-based environment, IDLE, distributed with Python. The command line history recall which operates within DOS boxes on NT and some other DOS and Windows flavors is yet another beast.


-13.1. 命令行编辑 Line Editing

If supported, input line editing is active whenever the interpreter prints a primary or secondary prompt. The current line can be edited using the conventional Emacs control characters. The most important of these are: C-A (Control-A) moves the cursor to the beginning of the line, C-E to the end, C-B moves it one position to the left, C-F to the right. Backspace erases the character to the left of the cursor, C-D the character to its right. C-K kills (erases) the rest of the line to the right of the cursor, C-Y yanks back the last killed string. C-underscore undoes the last change you made; it can be repeated for cumulative effect.

如果支持,输入行编辑功能活跃时,解释器打印一个小学或中学的提示。当前行可以被编辑使用传统的Emacs的控制字符。其中最重要的是::大骨节病:``的CA(控制一)移动光标到行的开头:大骨节病:行政长官`` 到最后,:大骨节病:``会CB它一个位置移动到左边:大骨节病:CF卡`到` 权利。 Backspace键擦除字符光标的左侧,:大骨节病:``的光盘字符的权利。 :大骨节病:``对照杀死(删除)的规定向其他行右边的光标:大骨节病:``洋基赛扬回到过去杀死字符串。:大骨节病:`的C -强调`撤消所做的最新变化,它可以被重复累积效应。

-13.2. 历史替换 History Substitution

History substitution works as follows. All non-empty input lines issued are saved in a history buffer, and when a new prompt is given you are positioned on a new line at the bottom of this buffer. C-P moves one line up (back) in the history buffer, C-N moves one down. Any line in the history buffer can be edited; an asterisk appears in front of the prompt to mark a line as modified. Pressing the Return key passes the current line to the interpreter. C-R starts an incremental reverse search; C-S starts a forward search.

历史替代的工作原理如下。所有非空输入行已发行保存在一个历史缓冲区,当一个新的提示是给你的定位在在此缓冲区的底部新行。 :大骨节病:`C -肽’的行动之一排队(回)在历史缓冲区:大骨节病:`碳氮`行动之一了。历史缓冲区中的任何行可以进行编辑;一个星号在前面出现的提示行作为标记修改。按:大骨节病:``键返回传递到当前行翻译。 :大骨节病:``华润启动增量逆向搜索;:大骨节病:政务司司长`启动` 向前搜索。

-13.3. 键绑定 Key Bindings

The key bindings and some other parameters of the Readline library can be customized by placing commands in an initialization file called ~/.inputrc. Key bindings have the form

键绑定和其他一些参数Readline库可自定义放置在一个初始化调用文件中的命令:文件:`~/. inputrc`。键绑定的形式为::

    key-name: function-name


"string": function-name

and options can be set with

set option-name value

For example:

# I prefer vi-style editing:
set editing-mode vi

# Edit using a single line:
set horizontal-scroll-mode On

# Rebind some keys:
Meta-h: backward-kill-word
"\C-u": universal-argument
"\C-x\C-r": re-read-init-file

Note that the default binding for Tab in Python is to insert a Tab character instead of Readline’s default filename completion function. If you insist, you can override this by putting

注意,默认绑定:大骨节病:``标签在Python是插入一个:大骨节病:``标签 Readline的性格,而不是默认的文件名完成功能。如果你坚持,你可以通过把这个命令::

    Tab: complete

in your ~/.inputrc. (Of course, this makes it harder to type indented continuation lines if you’re accustomed to using Tab for that purpose.)

Automatic completion of variable and module names is optionally available. To enable it in the interpreter’s interactive mode, add the following to your startup file: [1]

自动变量和模块名称竣工可供用户选择。要能够在翻译的互动模式下,添加以下到您的启动文件:[#] _::

    import rlcompleter, readline
    readline.parse_and_bind(‘tab: complete’)

This binds the Tab key to the completion function, so hitting the Tab key twice suggests completions; 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.

这个约束:大骨节病:``标签完成的关键功能,使击球:大骨节病:``制表键两次建议落成,它的名字看起来Python的语句,当前局部变量,可用模块的名称。对于点如`` `` string.a表达式,它会计算表达式到最后``’.’``然后建议从由此产生的属性落成对象。请注意,这可能会执行应用程序定义的代码,如果与一个对象:甲基:`__getattr__`方法是表达式的一部分。

A more capable startup file might look like this example. Note that this deletes the names it creates once they are no longer needed; this is done since the startup file is executed in the same namespace as the interactive commands, and removing the names avoids creating side effects in the interactive environment. You may find it convenient to keep some of the imported modules, such as os, which turn out to be needed in most sessions with the interpreter.

一个更强大的启动文件可能看起来像这样的例子。请注意,此删除它创建的名字一旦不再需要,这是自做启动文件中执行的命令相同的命名空间的互动,避免和消除的名字创造了互动的副作用环境。您可能会发现它方便地保持进口部分模块,例如:调制:`口`,它变成是最需要与会议翻译。 ::

# Add auto-completion and a stored history file of commands to your Python 
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
# Store the file in ~/.pystartup, and set an environment variable to point
# to it: “export PYTHONSTARTUP=/home/user/.pystartup” in bash.
# Note that PYTHONSTARTUP does not expand “~”, so you have to put in the
# full path to your home directory.

   import atexit
   import os
   import readline
   import rlcompleter

   historyPath = os.path.expanduser(“~/.pyhistory”)

   def save_history(historyPath=historyPath):
       import readline

   if os.path.exists(historyPath):

   del os, atexit, readline, rlcompleter, save_history, historyPath

-13.4. 交互式解释器的替代方案 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. It can also be thoroughly customized and embedded into other applications. Another similar enhanced interactive environment is bpython.

一种替代增强的交互式解释器,它已经有好周围一段时间是`_`IPython中,其特点tab完成,对象勘探和先进的历史管理。它也可以被彻底定制和嵌入到其他应用程序。另一个类似的增强的交互式环境 bpython

附录 Footnotes


Python will execute the contents of a file identified by the PYTHONSTARTUP environment variable when you start an interactive interpreter.


————— 返回《 Python 官方教程 》目录 —————
—— 返回《 Python 手册 》总目录 ——