[Python-ideas] for/except/else syntax

Andrey Fedorov anfedorov at gmail.com
Wed Oct 7 17:17:38 CEST 2009


Agree with Rob that the "else" keyword confusing in the context of a for
loop. In my mind, "for each pebble in the bag, give it to Ben, or else ..."
has no clear semantic meaning. What do you mean, "or else"?
In my mind, better words for what "else" currently does seem to be
"afterwards", "atend", "ending", "thereafter", or "subsequently".

- Andrey

On Wed, Oct 7, 2009 at 10:44 AM, Guido van Rossum <guido at python.org> wrote:

> On Tue, Oct 6, 2009 at 9:06 PM, Bruce Leban <bruce at leapyear.org> wrote:
> > Here you go:
> > do_except = True
> > for i in SEQ:
> >   do_except = False
> >   A
> > else:
> >   if not do_except:
> >     C  # suite_if_loop_is_not_broken
> > if do_except:
> >   B  # suite_if_loop_body_is_not_executed
>
> This doesn't keep the else semantics the same; currently in a for-loop
> over an empty list, the else clause is still executed, but not in your
> case, since do_except remains true. You may have meant this
> intentionally but it means that adding 'except: pass' would change the
> semantics of a for+else loop, which I find unexpected.
>
> I now understand that you want the except clause executed when there
> were zero iterations (your wording "not executed" didn't mean anything
> to me). Naming it 'except' is a really bad idea though because it
> makes people thing of exception handling.
>
> I can somewhat sympathize with this desire, but I think there are
> already plenty of ways to do this without adding new syntax. In
> general the addition of new syntax ought to be a truly rare event, and
> with the still pending transition to Python 3.x, doubly so.
>
> > --- Bruce
> >
> >
> > On Tue, Oct 6, 2009 at 8:53 PM, Guido van Rossum <guido at python.org>
> wrote:
> >>
> >> On Tue, Oct 6, 2009 at 8:46 PM, Bruce Leban <bruce at leapyear.org> wrote:
> >> > Switching gears a bit, here's what I want:
> >> >
> >> > for_stmt ::= "for" target_list "in" expression_list ":"
> suite_loop_body
> >> > ["except" ":" suite_if_loop_body_is_not_executed] ["else" ":"
> >> > suite_if_loop_is_not_broken]
> >> >
> >> > The except and else appear in this order to make it clear that except
> >> > takes
> >> > precedence. If the loop body is not executed at all then both
> conditions
> >> > are
> >> > true but it obviously is only useful if except takes precedence.
> >> > Comments?
> >>
> >> I'm sure it's obvious to you what the except clause should do, but it
> >> isn't to me. :-( Assuming you're not proposing we change the meaning
> >> of else in the absence of except, and that its meaning together with
> >> except is a logical extension of this, can you show how we should
> >> translate a for-loop with an except clause into current Python? Say,
> >> what would I be writing today to get the exact effect of your proposed
> >>
> >> for i in SEQ:
> >>  A
> >> except:
> >>  B
> >> else:
> >>  C
> >>
> >> ?
> >>
> >> --
> >> --Guido van Rossum (home page: http://www.python.org/~guido/)
> >
> >
>
>
>
> --
> --Guido van Rossum (home page: http://www.python.org/~guido/)
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20091007/0bd51c69/attachment.html>


More information about the Python-ideas mailing list