<br><br><div class="gmail_quote">On Wed, Oct 7, 2009 at 5:58 PM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info">steve@pearwood.info</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;">
<div class="im">On Thu, 8 Oct 2009 04:27:22 am MRAB wrote:<br>
<br>
> Perhaps:<br>
><br>
> for i in SEQ:<br>
>      A<br>
> elif not break:<br>
>      C # suite_if_loop_is_not_broken<br>
> elif pass:<br>
>      B  # suite_if_loop_body_is_not_executed<br>
<br>
</div>Here's my executive summary, for those in a hurry:<br>
<br>
-1 on any variation like "else not break" or "elif not break".<br>
<br>
+1 on aliasing "for...else" to "for...then", with identical semantics,<br>
with eventual deprecation and removal of for...else some time in the<br>
indefinite future. (Likewise for while...else.)<br>
<br>
-0 on introducing a clause that executes if the loop is never executed<br>
at all.<br>
<br>
<br>
And here are the details:<br>
<br>
Any variation like "else not break" or "elif not break" makes it look<br>
that you are testing a flag. If you think for...else is confusing<br>
now, I guarantee that this will be nothing compared to the confusion<br>
people will feel when they discover this (proposed) thing that looks<br>
like testing a flag but isn't. People will wonder where this break<br>
flag is set, and why they can't set it themselves, or even access it<br>
like any other object.<br>
<br>
(Before anyone suggests that perhaps we should introduce a global<br>
variable called "break", and allow the caller to read/write to it<br>
outside of the for loop, remember that break is a statement. It would<br>
be too abominable for words to have break a global variable and a<br>
statement at the same time.)<br>
<br>
Assuming there is consensus that for...else is confusing, my<br>
suggestion is to add an alias "then":<br>
<br>
for x in seq:<br>
    ...<br>
then:<br>
    suite<br>
<br>
The behaviour of "then" is identical to the current behaviour<br>
of "else", it's just another way of spelling it. Assuming consensus,<br>
eventually else would be deprecated and then removed.<br>
<br>
This has the advantage of a more natural interpretation. It seems more<br>
natural to say:<br>
<br>
    run the loop, then execute suite (unless there was a break)<br>
<br>
instead of<br>
<br>
    run the loop, optionally break, else execute suite.<br>
<br>
<br>
<br>
If you need to distinguish between "loop ran" and "loop didn't run at<br>
all", you don't need syntax for that, you can use the sentinel trick<br>
Ilya suggested. (You don't even need a special sentinel, just use<br>
None, provided you know None is not in the sequence you're iterating<br>
over.) But if this was so common a use-case that we needed syntax for<br>
it (and I don't believe it is), then I'd suggest:<br>
<br>
for x in seq:<br>
     suite A<br>
then:<br>
     suite B<br>
otherwise:<br>
     suite C<br>
<br>
I'm not really enamoured of "otherwise". I'd prefer to use "else" for<br>
the case of "the loop was never entered at all", but of course that<br>
is impossible given that it currently has another meaning.<br>
<font color="#888888"><br>
<br>
<br>
--<br>
Steven D'Aprano</font><br></blockquote><div><br>Sounds good to me.<br><br>Geremy Condra <br></div></div><br>