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

Steve D'Aprano steve+python at pearwood.info
Thu Nov 2 11:57:40 EDT 2017


On Fri, 3 Nov 2017 12:39 am, Jon Ribbens wrote:

> On 2017-11-01, Alexey Muranov <alexey.muranov at gmail.com> wrote:
>> what do you think about the idea of replacing "`else`" with "`then`" in
>> the contexts of `for` and `try`?
>>
>> It seems clear that it should be rather "then" than "else."  Compare
>> also "try ... then ... finally" with "try ... else ... finally".
>>
>> Currently, with "else", it is almost impossible to guess the meaning
>> without looking into the documentation.
> 
> Why would we want to make the language worse? It is fairly obvious
> what 'else' means, 

Yes, obvious and WRONG.

for x in seq:
    do_something()
else:
    print("seq was empty")

is an obvious, common and wrong interpretation.


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

Perhaps before we continue, we should ask what you think for...else and
while...else statements do. Just to be sure we are all on the same page here.


> It seems clear that 
> 'else' is the correct word (or at least, far better than 'then').

Not clear at all. The 'for...else' block is a common source of confusion, if
it was clear what it did, people wouldn't so often get it wrong.


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



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