nested loops
anton muhin
antonmuhin.REMOVE.ME.FOR.REAL.MAIL at rambler.ru
Thu Oct 9 06:22:54 EDT 2003
Oleg Leschov wrote:
> Could there be means of exiting nested loops in python?
> something similar to labelled loops in perl..
>
> I consider it irrating to have to make a flag for sole
> purpose of checking it after loop if we need to break once more...
>
> So maybe there should be an argument to break that is an int which is
> a number of loops to break. So it would default to 1 (or whatever means
> the current enclosing loop), not breaking any code...
>
> Or maybe name the loops and give the loop name argument to break:
> for i in list as outerloop:
> while 1:
> <do something>
> if u:
> continue
> elif v:
> continue outerloop
> elif w:
> break
> elif x:
> break outerloop
> Or both.
>
> Are there any cons to this proposal?
>
>
Not an ideal solution, but still might be of interest:
>>> for x in range(100):
... print x
... for y in range(100):
... print '\t', y
... if y == 40:
... break
... else:
... continue
... break
...
Actually, I'd rather abstract these complicated loops into a generator,
in my limited experience it's usually much simplier and clearer.
hth,
anton.
More information about the Python-list
mailing list