why no break N levels, as in other leading languages?

Evan Simpson evan at 4-am.com
Fri May 2 11:48:11 EDT 2003


Aahz wrote:
> class BreakException(Exception):
>     pass
> 
> try:
>     for i in range(foo):
>         for j in range(bar):
>             for k in range(spam):
>                 if my_func():
>                     raise BreakException
> except BreakException:
>     pass

This reminds me of a proposal that I actually coded up a Python patch 
for, then lost:

http://groups.google.com/groups?threadm=CXxz3.57%242y4.69103%40news-dal.corridex.com

It allowed the above to be written as:

try:
     for i in range(foo):
         for j in range(bar):
             for k in range(spam):
                 if my_func():
                     break 'Matched'
continue 'Matched':
     pass

This is more than syntactic sugar, since the association between the 
break statement and the continue clause is completely lexical and static.

I realize that the 'continue' keyword syntax for the break target isn't 
great, but I couldn't think of a better one.

Cheers,

Evan @ 4-am







More information about the Python-list mailing list