<div dir="ltr"><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; white-space: pre; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><div>
On python-dev the subject of for/else statements came up, so I had to mention how "ambiguous" the syntax seems to me. By "ambiguous" I meant that it's not obvious what should happen in the for/else and while/else constructs (the except/else construct is readable and great imo btw).</div>
<div><br></div><div>Even though it's documented, for me it's a bad construct because it can and will always be misinterpreted by a non-trivial amount of people seeing it for the first time. It's unreadable and confusing because the "else" isn't referring to the "for" it's in reference to the "break". The construct is only useful in loops with "break" statements, so in pseudo code or human the "else" would have probably been translated to "if didn't break".</div>
<div><br></div><div>To make things worse, it's not that newcomers will say "hmm, what does this for/else thing do? lets look at the manual", they will mistakenly assume they understand what the construct means. For evidence, see the for/else construct in the django templating language where "else" means the loop didn't run. The django interpretation IMHO is much more natural because "else" AFAIK means "otherwise" meaning that statements inside an "else" block will never be executed if the statements in the previous block were. </div>
<div><br></div><div>To summarize:</div><div><br></div><div>whatever:</div><div> A</div><div>else:</div><div> B</div><div><br></div><div>looks like it means either A happened or B happened. In python for/while/else constructs, this doesn't hold.</div>
<div><br></div><div>--yuv</div><div><br></div>Original Thread: [Python-Dev] Announcing PEP 3136</span><br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Terry Reedy</b> <span dir="ltr"><<a href="mailto:tjreedy@udel.edu">tjreedy@udel.edu</a>></span><br>
Date: Thu, Oct 1, 2009 at 2:47 AM<br>Subject: Re: [Python-Dev] Announcing PEP 3136<br>To: <a href="mailto:python-dev@python.org">python-dev@python.org</a><br><br><br><div class="im">Yuvgoog Greenle wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I like how python has a minimalistic and powerful syntax (-1 for the break ___ PEP).<br>
<br>
Also, I really dislike the for/else ambiguity "butterflies".<br>
</blockquote>
<br></div>
Properly understood, no ambiguity.<br>
<br>
while c:<br>
x()<br>
<br>
is equivalent to hypothetical<br>
<br>
label z:<br>
if c:<br>
x()<br>
goto: z<br>
<br>
So<br>
<br>
while c:<br>
x()<br>
else:<br>
y()<br>
<br>
is equivalent to<br>
<br>
label 1:<br>
if c:<br>
x()<br>
goto: 1<br>
else"<br>
y()<br>
<br>
The else clause fires (once) if and when the if/while condition evaluates as false. Break and continue are restricted *unconditional* goto statements, and so *cannot* trigger an else clause.<br>
<br>
In for loops, the implied condition is 'there is another item in the collection represented by the iterable'.<br>
<br>
For any more, move to another list.<br>
<br>
Terry Jan Reedy<div><div></div><div class="h5"><br>
<br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-dev" target="_blank">http://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="http://mail.python.org/mailman/options/python-dev/ubershmekel%40gmail.com" target="_blank">http://mail.python.org/mailman/options/python-dev/ubershmekel%40gmail.com</a><br>
</div></div></div><br></div>