[Python-ideas] Loop labels
David Townshend
aquavitae69 at gmail.com
Fri Mar 9 06:06:47 CET 2012
The biggest problem is not the new syntax. It's the new type of object
that would be needed, Label, which lies in some magical place half way
between a name and a keyword. What would be the result of the following
code?
loops = []
for i in range(4) as label:
print(type(label), dir(label))
loops.append(label)
for label in loops as newlabel:
break label
David
On Mar 9, 2012 1:08 AM, "Westley MartÃnez" <anikom15 at gmail.com> wrote:
> On Fri, Mar 09, 2012 at 02:52:08AM +0800, Matt Joiner wrote:
> > Several languages allow break and continue statements to take a count
> > or loop label for greater flexibility.
> >
> > Dealing with nested loops and control flow around them is definitely
> > something I and probably most programmers deal with everyday.
> > Generally for very complex control flow one might employ functions,
> > and use return statements to work around any shortcomings. This is not
> > always ideal in CPython because of the performance cost of function
> > calls, and lack of anonymous functions. The other work around is
> > usually goto statements, which clearly aren't available or appropriate
> > in Python.
> >
> > So what about the following extensions?
> >
> > Allow for and while statements to be labelled using "as".
> >
> > Allow break and continue to take the name of a containing loop, or an
> > integer. The corresponding named loop, or the nth containing loop are
> > treated as though they are the nearest enclosing loop.
> >
> > loop_label ::= identifier
> >
> > break_stmt ::= "break" [decimalinteger | loop_label]
> >
> > continue_stmt ::= "continue" [decimalinteger | loop_label]
> >
> > while_stmt ::= "while" expression ["as" loop_label] ":" suite
> > ["else" ":" suite]
> >
> > for_stmt ::= "for" target_list "in" expression_list ["as" loop_label]
> ":" suite
> > ["else" ":" suite]
> >
> > Here's a really naive example:
> > for a in b as c:
> > for d in e:
> > break c # or break 2
> > for f in g:
> > continue c # or continue 2
>
> -1; if we do this we might as well add goto labels.
> _______________________________________________
> 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/20120309/a14ef2ae/attachment.html>
More information about the Python-ideas
mailing list