<br><br><div class="gmail_quote">On Thu, Oct 8, 2009 at 7:35 PM, Gerald Britton <span dir="ltr"><<a href="mailto:gerald.britton@gmail.com">gerald.britton@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Ach,  I just whipped it up in a hurry.  I didn't pull it from live<br>
code (or even test it).   A better (working) version:<br>
<br>
>>> x = []<br>
>>> del i<br>
>>> for i, j in x:<br>
...   do_something()<br>
... else:<br>
...   i = vars().get('i',-1)<br>
...<br>
>>> i<br>
-1<br>
>>><br>
<br>
And before someone says _again_ that the else is not necessary here,<br>
that's not the reason I use it.  I use it to structure the code so<br>
that it is obvious that the stuff in the else clause belongs to the<br>
for loop, not to code that follows it.<br>
<br>
The point is, there are valid reasons to use this construct.  That's<br>
just one of many, many more.  I would fight tooth-and-nail against a<br>
warning message, if I thought there was even the faintest chance of it<br>
being considered.  As it is, there is essentially no chance that it<br>
will be considered for the compiler (pylint is another matter,  I<br>
think that it _should_ be there).<br></blockquote><div><br>I'm not clear- you're saying its a code smell to use it that way?<br><br>Geremy Condra<br> <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

<div><div></div><div class="h5"><br>
On Thu, Oct 8, 2009 at 6:52 PM, Yuvgoog Greenle <<a href="mailto:ubershmekel@gmail.com">ubershmekel@gmail.com</a>> wrote:<br>
> Gerald, if this warning would have been generated you might have saved<br>
> yourself the mistakes on the previous thread.<br>
><br>
> Gerald wrote as an example of "for..else" being useful without a break:<br>
>> for i, j in enumerate(something):<br>
>>  # suite<br>
>>  i += 1<br>
>> else:<br>
>>  i = 0<br>
>><br>
>> # i == number of things processed<br>
><br>
> If "# suite" has no break, "i" will always be 0. Either way after the<br>
> loop "i" isn't going to have the number of things processed (unless<br>
> "something" was empty). After being corrected Gerald wrote the<br>
> following remedied example:<br>
><br>
>> for i, j in enumerate(something):<br>
>>  # do something<br>
>>  i += 1<br>
>> else:<br>
>>  i = -1<br>
>><br>
>> if i > 0:<br>
>>  # we did something<br>
><br>
> The last block "# we did something" is unreachable if "#do something"<br>
> has no break.<br>
><br>
> This isn't meant to go against Gerald here btw. I'm simply saying that<br>
> if even the best Pythonistas can cause havoc with "for...else" then<br>
> think about the noobs. Personally I would suggest not using the<br>
> "for...break...else" idiom at all but the least we can do is give a<br>
> warning when "for..else" is so obviously misused.<br>
><br>
> You could allow "break" outside of loops and simply ignore it, but you<br>
> don't allow it because it helps avoid bugs. Really, a warning is the<br>
> LEAST python can do here.<br>
><br>
> --yuv<br>
><br>
<br>
<br>
<br>
</div></div>--<br>
<font color="#888888">Gerald Britton<br>
</font><div><div></div><div class="h5">_______________________________________________<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>