break levels

Alex Martelli aleaxit at yahoo.com
Mon Apr 16 16:52:21 EDT 2001


"Benjamin.Altman" <benjamin.altman at noaa.gov> wrote in message
news:3ADB447A.56CE45AA at noaa.gov...
> Is there a particular reason that Python can't break over multiple
> loops?  Example:
>     for i in range(0,10):
>         for j in range(0,10):
>             if some_condition:
>                 break(2)
>
> which would break out from the inner and outer loop.  This would be
> similar to the Bourne shell usage of break.  I suppose you could do a
> try and throw an exception instead but it seems that sometimes doing a
> break(x) would be convenient.

See the recent 'named blockes' (sic) thread, where the same
functionality was desired the Java way (giving a loop a name,
and using the name on the break, rather than using a number
on the break -- much more readable and robust, IMHO).

Besides exceptions, make-into-a-function (since _return_
can then be used to break out of the nested loops that were
made into a function) is also a workaround here (although
in some cases I agree that multi-level break _would_ be handy).


Alex






More information about the Python-list mailing list