comments and the continuation prompt

Steve D'Aprano steve+python at pearwood.info
Mon Jun 26 08:53:06 EDT 2017


On Mon, 26 Jun 2017 01:32 pm, Ben Finney wrote:

> Steve D'Aprano <steve+python at pearwood.info> writes:
> 
>> On Mon, 26 Jun 2017 08:44 am, Stefan Ram wrote:
>>
>> >   According to The Python Language Reference Release 3.6.0, 2.1.3
>> >   Comments, »A comment signifies the end of the logical line unless
>> >   the implicit line joining rules are invoked.«.
>> > 
>> >   So, why do I get a continuation prompt when I enter a comment?
>>
>> Why not? As far as the interactive interpreter is concerned, you
>> haven't yet entered a statement.
> 
> And yet, according to the Language Reference, the logical line has ended
> and another begun.

Correct. That doesn't contradict anything I said.


> So I think the question is worth exploring: Why does the interactive
> prompt imply the logical line is continuing, when the Language Reference
> would say otherwise?

What leads you to believe that the prompt implies the logical line is
continuing? That's not implied by the prompt. I gave examples where the
secondary prompt is displayed that have nothing to do with logical lines:

py> def func():
...     x = 1
...     y = 2
...


That's not one logical line, and the presence of the secondary prompt ps2 does
not imply that it is.

The same applies to class, try, for, while, with, triple-quoted strings, and
possibly others. So why are we (by which I mean *not me*) thinking that the ps2
secondary prompt implies a single logical line?

All logical line continuations are displayed with the secondary prompt. This
does not imply that every time the secondary prompt is displayed, the line is
being continued.

Analogy: all people called "Ben" are human beings, but not all human beings are
called "Ben".

There is no puzzle here. The secondary prompt displayed after a comment is a
quirk of the CPython REPL, it isn't part of the language and it doesn't need to
be fixed because it isn't broken.

- IDLE doesn't do this.

- IPython doesn't do this:

In [1]: #comment

In [2]:


- Jython 2.5 doesn't do this:

>>> # comment
>>>

- Neither does bpython.

- I no longer have IronPython installed on any of my systems, so I can't check
that, or PyPy.

- The emulated REPL provided by the `code` module doesn't do it either:

py> import code
py> code.interact()
Python 3.5.2 (default, Oct 12 2016, 10:47:40)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-55)] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
py> # comment
py>



It's a quirk of the CPython default REPL, of no consequence and with no
implications, and it especially has nothing to do with line continuations.


> Maybe the answer is “the continuation prompt does not prompt for the
> continuation of a logical line, 

Sometimes it does.


> but the continuation of <something else>”.
> 
> What exactly goes in the “<something else>” placeholder; that is,
> exactly what should the user understand by that transition from one
> prompt to a different one?

A nested block; a triple-quoted string; a line continuation; a statement after a
comment; whatever reasonable thing a REPL chooses.

The language does not, so far as I am aware, mandate when the ps1 and ps2
prompts are used, and there is no basis for drawing any conclusion about the
Python language from the choices made by any specific REPL -- not even the
default CPython REPL.

https://docs.python.org/3/library/sys.html#sys.ps1




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list