<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">they wouldnt be equivalent if #staff in version 1 did not cointain "break" statement and this is common mistake</span><br>
<br><div class="gmail_quote">On Wed, Jan 23, 2013 at 1:39 AM, Oscar Benjamin <span dir="ltr"><<a href="mailto:oscar.j.benjamin@gmail.com" target="_blank">oscar.j.benjamin@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On 22 January 2013 23:41, Terry Reedy <<a href="mailto:tjreedy@udel.edu">tjreedy@udel.edu</a>> wrote:<br>
> On 1/22/2013 3:09 PM, Ethan Furman wrote:<br>
>><br>
>> On 01/22/2013 09:44 AM, Terry Reedy wrote:<br>
>>><br>
[SNIP]<br>
>>> The else clause is executed if and when the condition is false.<br>
>>> Now use a real Python while statement to do the *same<br>
>>> thing*.<br>
>>><br>
>>> while n > 0:<br>
>>>    n -= 1<br>
>>> else:<br>
>>>    n = None<br>
>><br>
>><br>
>> I understand how it works (although it did take a while for it to sink<br>
>> in); my gripe, and probably why it is misunderstood so often, is that<br>
>> nine times out of ten when I /want/ to use a while-else or for-else I<br>
>> only want the true/false check /once/, at the beginning of the loop.<br>
><br>
><br>
> I do not understand what you are saying. There already is only one<br>
> true/false check, at the beginning of the loop. If you only want the check<br>
> *performed* once, you would use if-else. But I presume you know this.<br>
<br>
I think he meant that he would use the else clause more often if it<br>
had the semantics so that the two blocks below were equivalent:<br>
<br>
# Version 1<br>
while condition:<br>
    # stuff<br>
else:<br>
    # other stuff<br>
<br>
# Version 2<br>
if condition:<br>
    while condition:<br>
        # stuff<br>
else:<br>
    # other stuff<br>
<br>
So he wants a convenient way to execute code only if the loop<br>
performed zero iterations. I think that often when people are confused<br>
about the else clause on while loops it is because they expect this<br>
behaviour (which would also be useful). The same confusion arises with<br>
for loops where people expect the else clause to execute if the<br>
iterable was empty so that these would be equivalent:<br>
<br>
# Version 1<br>
for x in iterable:<br>
    # stuff<br>
else:<br>
    # other stuff<br>
<br>
# Version 2<br>
iterated = False<br>
for x in iterable:<br>
    iterated = True<br>
    # stuff<br>
if not iterated:<br>
    # other stuff<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
Oscar<br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br>