[Python-Dev] for...else

Rob Cliffe rob.cliffe at btinternet.com
Tue Jul 25 06:28:38 EDT 2017



On 25/07/2017 06:51, Nick Coghlan wrote:
> On 25 July 2017 at 02:23, Ben Hoyt <benhoyt at gmail.com> wrote:
>> This is more of a python-ideas discussion, and Steven's answer is good.
>>
>> I'll just add one thing. Maybe it's obvious to others, but I've liked
>> for...else since I found a kind of mnemonic to help me remember when the
>> "else" part happens: I think of it not as "for ... else" but as "break ...
>> else" -- saying it this way makes it clear to me that the break goes with
>> the else. "If this condition inside the loop is true, break. ... *else* if
>> we didn't break, do this other thing after the loop."
> For folks looking for a more in-depth explanation of the
> "if-break-else" approach to thinking about this construct:
> http://python-notes.curiousefficiency.org/en/latest/python_concepts/break_else.html
A helpful explanation.
But that it is necessary at all underlines that (IMHO) this use of 
'else' is unnatural and hard to understand.  I always have to think 
twice about it, whether reading it or using it myself.  Therefore I 
would have preferred a more obvious keyword such as 'ifnobreak' (others 
may think of something better).
But as has been stated, it's not going to change.
Rob Cliffe

>
> That article also has a note explaining that we're unlikely to ever
> change this: http://python-notes.curiousefficiency.org/en/latest/python_concepts/break_else.html#but-couldn-t-python-be-different
>
> Cheers,
> Nick.
>



More information about the Python-Dev mailing list