[Python-ideas] Inconsistent script/console behaviour

Guido van Rossum guido at python.org
Thu Dec 15 20:07:07 CET 2011

On Thu, Dec 15, 2011 at 3:41 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:

> There are two steps to what the interactive console does:
> Step 1: accept a block of code.
> Step 2: compile and execute it in single statement mode (in order to
> get the result of any expressions for display)
> It is the *first* step that is terminated by a blank line. That step
> understands almost nothing about Python syntax - just enough to figure
> out whether or not it should be asking for a continuation line. (IIRC,
> it pretty much just does bracket matching and "if I have seen a ':'
> character, ask for continuation lines until one of them is blank".

Actually, in CPython at least, the first step uses a special feature of the
parser that can tell whether what has been entered so far is complete or
not. Try entering a subtle syntax error:

>>> def foo():
...   x = 1
...   y x
  File "<stdin>", line 3
    y x
SyntaxError: invalid syntax

It does treat an entirely blank line (with no comment or whitespace) as a
dedent all the way back to zero.

The problem with allowing what Anatoly asks for is that the added line
could introduce another indented block instead of just a statement.

Almost all languages with an interactive mode have some kind of syntactic
awkwardness or differences. E.g. I recently had problems with Haskell's
interactive mode, which won't let you define functions directly -- you must
use 'let'; and sqlite3 reads anything until you enter a semicolon. Trying
to mess with Python's approach is likely to make things worse in some other
way. If you think otherwise, show us the patch.

--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111215/20bdab5e/attachment.html>

More information about the Python-ideas mailing list