Breaking out of nested loops
Dietmar Schwertberger
dietmar at schwertberger.de
Sat Jan 12 13:57:22 EST 2002
In article <mailman.1010847137.7299.python-list at python.org>, Justin Sheehy
<URL:mailto:justin at iago.org> wrote:
> "Tal Linzen" <pachy at 012.net.il> writes:
> > One feature I miss in python is the ability to break out of nested loops.
> > I've searched the newsgroup archives, and found a few threads discussing the
> > problem, with no apparent conclusion. The two solutions I've seen -- raising
> > an exception, or putting the loop inside a function and using the return
> > statement -- are unintuitive (meaning, I didn't come up with them myself
> > :-) ).
> > Know if anyone has drafted a PEP for a syntactic solution for this problem?
> Who needs a syntactic solution now that you've found two very good
> ways of doing this that already exist?
> Both of the strategies you mention are well-known and very effective.
> New syntax is only added when there's a really good justification or
> when the new syntax is "print >>".
> Further, the print syntax addition is harmless even if it is
> unneccesary and ugly, as it doesn't use up a keyword and can't break
> any existing working code.
IMHO "break n" (default n=1) wouldn't be ugly, at least less ugly than
the workarounds...
Example:
for x in range(10):
for y in range(10):
for z in range(10):
if abc(x,y,z):
break 3
Regards,
Dietmar
More information about the Python-list
mailing list