for / while else doesn't make sense

Jon Ribbens jon+usenet at unequivocal.co.uk
Fri May 20 20:24:50 EDT 2016


On 2016-05-20, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 05/20/2016 04:55 AM, Jon Ribbens wrote:
>> Certainly. "else:" is (almost?) invariably used in the situation where
>> you are iterating through something in order to find a value which
>> matches a certain condition. So the "for:" block means "find this
>> value" and the "else:" means "else do this".
>
> I'm happy that you have a working mental model for for/else (seriously, 
> I am) but please don't discount the confusion and consternation for the 
> many folks who don't start with that mental model.
>
> The number of times I have /wanted/ to use the for/else structure for 
> searching is small (and I remember them both ;) -- every other time what 
> I wanted was an _else_ that ran iff the iterable was already empty when 
> the _for_ encountered it.

Well that's not a circumstance that's appropriate for the construct.
Saying you want a different construct entirely is an entirely
different argument.

>> I would find that very confusing. "then:" makes it sound like
>> executing that block is the usual case, when in practice it is
>> usually the exception - the fallback code if the expected value
>> was not found.
>
> If you don't take the extra step of _break_ it is the usual case.

Having an "for: else:" clause without a "break" would be so unusual
that it's literally nonexistent, because it would always be a bug.
So no, it isn't the usual case for "for: else:".

> But as others have said, this isn't going to change now, and I'm okay 
> with that.  But, please, be a bit more understanding of those who don't 
> immediately grok the for/else and while/else loops.

You're misunderstanding me. I'm not saying that the meaning of
"for: else:" is 100% intuitively obvious. I'm saying that it's
*more* obvious than it would be if it used any of the other existing
keywords. I suppose I'm also saying that there isn't any other
obvious word that could be made into a keyword that would be better
than "else" (even if we assumed that adding a new keyword was a
cost-free exercise).



More information about the Python-list mailing list