The "loop and a half"

Steve D'Aprano steve+python at pearwood.info
Sat Oct 7 04:35:08 EDT 2017


On Sat, 7 Oct 2017 11:05 am, bartc wrote:

> On 07/10/2017 00:43, Steve D'Aprano wrote:
>> On Sat, 7 Oct 2017 12:24 am, bartc wrote:
>> 
>>> print ("Enter blank expression to quit.")
>> 
>> 
>> I *despise* programs that do that, and would cheerfully and
>> unapologetically take their designers, disguise them as a lettuce, and
>> stake them out to be nibbled to death by snails.
>> 
>> At the interactive prompt, I am frequently hitting Enter on a blank line,
>> either by accident, or deliberately to break up the calculations into
>> groups, or just to give myself time to think.
>> 
>> Blank lines should be treated as "do nothing" and simply ignored, and there
>> should be an explicit QUIT command.
> 
> 
> Um, that actually follows what interactive Python does.


What is "that" to which you refer?

If you mean, "what I, Bart C, suggested, namely having the program exit on a
blank line", then you are wrong. In the Python interactive interpreter, you
can enter blank lines, and the interpreter doesn't exit.

If you mean "what Steven suggests, namely ignoring blank lines and requiring
either an explicit EOF (Ctrl-D on Unix) or quit() or exit() command to exit",
then you are correct, Python does do that.

There's one priviso: blank lines are only ignored at the primary prompt >>>
not the secondary prompt ... where a blank line is used to end the block. But
the interpreter doesn't exit. A slight compromise for the sake of convenience
at the interactive prompt.


> (Most of my real interactive programs with a command line interface
> programs use Escape, quit, exit, q or x to finish.

And how do you distinguish between calling quit from inside the function, and
using quit inside the function?

def foo():
    pass
    quit
    # or exit, q or x for that matter

would be a perfectly legal (although rather pointless) function.

If typing the letter x alone was enough to exit the block, how do you type
`x=1` when the instance you press `x` the interpreter helpfully exits the
block?

If you require `x` followed by ENTER, then that prohibits legal lines which
consist of nothing but `x`.

Unfortunately ESCAPE is already used. VT100 (the terminal emulation which is
used in just about all terminals) all control sequences begin with ESC. So
every time you do something like press an arrow key, the terminal sends ESC
followed by other stuff. It would be painful if every time you hit an arrow
key, the interpreter took it as "Exit".


> Interactive Python requires quit() or exit(), complete with parentheses.
> Unless you've redefined quit and exit as something else, then you have
> to crash out by other means.)

"Crash out", he says.

If your intention is to prove that you're a know-nothing ignoramus, you're
doing an excellent job of it.

 

-- 
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