<br><br><div class="gmail_quote">On Fri, Oct 2, 2009 at 9:18 PM, Carl Johnson <span dir="ltr"><<a href="mailto:cmjohnson.mailinglist@gmail.com">cmjohnson.mailinglist@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;">
Those who believe that the for/else construct is sufficiently clear,<br>
please read this email again:<br>
<br>
Gerald Britton:<br>
<div><div></div><div class="h5"><br>
> I can't imagine why I'm still commenting on this thread, since there<br>
> is no chance that Python will remove the "else" from for/while or put<br>
> conditions on it, but here is an example of a use that has no break<br>
> statement:<br>
><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>
> The thing here is, "i" won't be set by the "for" statement if<br>
> "something" is empty.  OTOH, if "something" is non-empty, i >= 1 at<br>
> the end of the for-loop.  Using the "else" clause in this way ensure<br>
> that "i" has the number of things processed at the end of the loop.<br>
><br>
> To do this without the "else" I have to:<br>
><br>
> i = 0<br>
> for i, j in enumerate(something):<br>
>  # suite<br>
>  i += 1<br>
> I can't imagine why I'm still commenting on this thread, since there<br>
> is no chance that Python will remove the "else" from for/while or put<br>
> conditions on it, but here is an example of a use that has no break<br>
> statement:<br>
><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>
> Does it work?  Sure!  But it moves the setting of "i" outside the<br>
> for-construct, which I personally dislike.<br>
<br>
</div></div>I don't mean to pick on Gerald, but he got the meaning of for/else<br>
dead wrong *in the middle of a debate about for/else*. This shows that<br>
even experienced Pythoneers who read Python-ideas can be confused<br>
about the meaning of for/else. Using it in production code means<br>
leaving a trap for the programmers who come after you and read your<br>
code to get confused and create a bug. Yes, when the others turn your<br>
working use of for/else into a bug, it's their fault for not reading<br>
the tutorial more closely so as not get confused by it, but ultimately<br>
no matter how much you blame the user, that won't stop the next guy<br>
who uses your code from misunderstanding it.<br></blockquote><div><br>This seems to be an argument for better documenting this behavior,<br>rather than an argument against the behavior per se. <br></div><div> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

The best solution to this inherent confusingness is to leave bare<br>
for/else alone for the foreseeable future, but to add a less confusing<br>
synonym for this feature. I propose "else not break:" although "elif<br>
not break:" is good too. Rewriting Gerald's example as<br>
<div class="im"><br>
for i, j in enumerate(something):<br>
  # suite<br>
  i += 1<br>
</div>else if not break:<br>
<div class="im">  i = 0<br>
<br>
# i == number of things processed<br>
<br></div></blockquote><div><br>To me its this is even less intuitive than the bare else- first you<br>keep that, then you commandeer "if not break" to mean "if this<br>didn't break". There may be a better syntax out there, but no<br>
offense, I don't see this as being it.<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 class="im">
</div>makes it 100% smack you in the face obvious that "Oh yeah, this is<br>
going to set i to 0 every time, since there's no break."<br>
<br>
In an unrelated but also good idea, it would be nice to be able to do<br>
what Gerald thought he was doing:<br>
<div class="im"><br>
for i, j in enumerate(something):<br>
  # suite<br>
  i += 1<br>
</div>else if None: #or some other suitable phrase for "empty"<br>
<div class="im">  i = 0<br>
<br>
# i == number of things processed<br>
<br>
</div><font color="#888888">— Carl<br>
</font><div><div></div><br></div></blockquote><div><br>Again, not my cup of tea. Others will doubtless disagree.<br><br>Geremy Condra <br></div></div>