<div dir="ltr">Thank god for empirical evidence and thanks Antti for the measurement. This construct should have been named "finally", "didnt_break" or "not break":<div><br></div><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">while self.more_points_to_check():</span></div>
<span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">    if self.found_it():<br>        # Found the desired point<br>        break</span><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">not break:<br>
    # Exhausted the search space :(</span><div><div><br></div><div><br></div><div><div class="gmail_quote">On Thu, Oct 1, 2009 at 2:00 PM, Antti Rasinen <span dir="ltr"><<a href="mailto:ars@iki.fi">ars@iki.fi</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im"><br>
> Yuvgoog Greenle schrieb:<br>
>> On python-dev the subject of for/else statements came up, so I had to<br>
>> mention how "ambiguous" the syntax seems to me. By "ambiguous" I meant<br>
>> that it's not obvious what should happen in the for/else and while/else<br>
>> constructs (the except/else construct is readable and great imo btw).<br>
>><br>
>> Even though it's documented, for me it's a bad construct because it can<br>
>> and will always be misinterpreted by a non-trivial amount of people<br>
>> seeing it for the first time. It's unreadable and confusing because the<br>
>> "else" isn't referring to the "for" it's in reference to the "break".<br>
>> The construct is only useful in loops with "break" statements, so in<br>
>> pseudo code or human the "else" would have probably been translated to<br>
>> "if didn't break".<br>
><br>
> I agree that the "else" clause is somewhat unintuitive.  However, it has<br>
> its uses and can often replace more awkward constructs.  As such, I would<br>
> be -1 on removing it, and -3 on reusing the else clause for some other<br>
> purpose.<br>
<br>
</div>I must say that calling for ... else "somewhat unintuitive" is the<br>
understatement of the year. The analogy with if ... else leads further<br>
astray than Odysseus.<br>
<br>
The feature itself is not bad. It's just that it is misnamed.<br>
<br>
I did a quick poll at the office and at IRC, five people in total, with<br>
2-8 years of Python experience. Two had never heard of the for..else<br>
-construct; one had heard that it exists and two know about it but have<br>
never used it. Three uninitiated, in other words.<br>
<br>
All three gave similar replies: if the loop is not executed or the<br>
variable is not iterable, then the else-branch is executed. Here are their<br>
reactions, when they learned how it really works:<br>
<br>
  * "It should be called for..then" (or for..finally)<br>
  * "WHAT. No f** way. Completely unintuitive"<br>
  * "I'd hate whoever wrote that code"<br>
<br>
These people are actual Python developers, not some newbies. And I<br>
completely agree with them. I've only once seen for...else in the wild and<br>
never used it myself.<br>
<br>
To add further insult to the naming injury, the else branch suffers from<br>
the fact that 99.9% of the time, the for-loop contains an if:<br>
<br>
for x in xs:<br>
    if cond(x):<br>
        break<br>
    # stuff<br>
else:<br>
    # more stuff<br>
<br>
My code pattern matching algorithm reads that as an indentation error. An<br>
else matches if so many times more often that decoding for..else requires<br>
unnecessary effort.<br>
<br>
I don't want the feature to be removed or even renamed (at least before<br>
Python 4). But the feature *is* badly named and unintuitive.<br>
<font color="#888888"><br>
--<br>
[ Antti Rasinen <*> <a href="mailto:ars@iki.fi">ars@iki.fi</a> ]<br>
</font><div><div></div><div class="h5"><br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</div></div></blockquote></div><br></div></div></div></div>