replacing `else` with `then` in `for` and `try`

Steve D'Aprano steve+python at pearwood.info
Thu Nov 2 21:02:19 EDT 2017


On Fri, 3 Nov 2017 03:31 am, Jon Ribbens wrote:

> On 2017-11-02, Steve D'Aprano <steve+python at pearwood.info> wrote:
>> On Fri, 3 Nov 2017 12:39 am, Jon Ribbens wrote:
>>> Why would we want to make the language worse? It is fairly obvious
>>> what 'else' means,
>>
>> Yes, obvious and WRONG.
> 
> Nope, obvious and right.
>
>> for x in seq:
>>     do_something()
>> else:
>>     print("seq was empty")
>>
>> is an obvious, common and wrong interpretation.
> 
> No, it's an obvious bug. You have a 'for...else' with no 'break'.
> Like I said, that should probably be a syntax error.

It should absolutely not be a syntax error. There's no reason for it to be a
syntax error, except to satisfy some arrogant and foolish idea of purity.

There are uncountable ways of writing code which is seemingly "pointless", and
we don't make it a syntax error. Sometimes it is even useful. A for...else
with no break should no more be a syntax error than "if True".


>>> whereas 'then' has an obvious meaning that is in
>>> fact the opposite of what it would actually do.
>>
>> Er... is today opposite day? Because 'then' describes precisely what it
>> actually does.
> 
> No, 'then' describes the opposite of what it does. The word 'then'
> implies something that always happens next, 

Right, which is what happens with the for...else block.

The else block is executed after the for loop, unless you jump out of the
statement using return, raise or break. The flow is:

    # this is what actually happens
    loop
    then `else` block

rather than:

    # this is incorrect
    loop
    else (otherwise) `else` block

which implies that that `else` block runs if the loop did not.


> whereas 'else' conveys 
> the correct meaning, which is something that happens if the course
> of the preceding piece of code did not go as expected.

That's not what happens. This is WRONG:

for x in sequence:
    ...
else:
    print("something unexpected happened")


Apart from the impossibility of deciding what "unexpected" means in general,
the behaviour is the opposite. The `else` clause executes immediately after
you fall out the bottom of the for-loop, NOT conditional on some event
(whether unexpected or not).


>>> Maybe the change should be that it is a syntax error to use a
>>> 'for/while...else' with no 'break'.
>>
>> Only if you want to make the experience of using Python in the interactive
>> interpreter worse. See my recent post:
>>
>> "A use-case for for...else with no break"
> 
> Yes, I saw that. It's possible you are the only person in the world
> ever to have done that. It would not make the interactive interpreter
> 'worse' in the slightest for that silly trick to be lost.

Just because you personally haven't used this technique doesn't make it
a "silly trick".

And while I thank you for the complement that I am the cleverest and most
insightful Python coder in the world, I'm sure I'm not. If I could think of
this technique, I daresay that many other people who are much smarter than me
have done so too.



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