interactive.rst 2.36 KB
Newer Older
1 2 3 4 5 6 7 8
.. _tut-interacting:

**************************************************
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
Georg Brandl's avatar
Georg Brandl committed
9
the GNU Bash shell.  This is implemented using the `GNU Readline`_ library,
10 11
which supports various styles of editing.  This library has its own
documentation which we won't duplicate here.
12 13 14 15


.. _tut-keybindings:

16 17
Tab Completion and History Editing
==================================
18

19 20 21 22 23 24 25 26 27 28 29 30
Completion of variable and module names is
:ref:`automatically enabled <rlcompleter-config>` at interpreter startup so
that the :kbd:`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 :meth:`__getattr__` method
is part of the expression.  The default configuration also saves your
history into a file named :file:`.python_history` in your user directory.
The history will be available again during the next interactive interpreter
session.
31 32 33 34


.. _tut-commentary:

Georg Brandl's avatar
Georg Brandl committed
35 36
Alternatives to the Interactive Interpreter
===========================================
37 38 39 40 41 42 43 44

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.

Georg Brandl's avatar
Georg Brandl committed
45
One alternative enhanced interactive interpreter that has been around for quite
46
some time is IPython_, which features tab completion, object exploration and
Georg Brandl's avatar
Georg Brandl committed
47 48
advanced history management.  It can also be thoroughly customized and embedded
into other applications.  Another similar enhanced interactive environment is
49
bpython_.
Georg Brandl's avatar
Georg Brandl committed
50

51

52
.. _GNU Readline: https://tiswww.case.edu/php/chet/readline/rltop.html
53
.. _IPython: https://ipython.org/
Georg Brandl's avatar
Georg Brandl committed
54
.. _bpython: http://www.bpython-interpreter.org/