By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

nested loops

P: n/a

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?
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Dans l'article <bm**********@ocasysi.rubbernet.net>, Oleg Leschov a écrit :

Could there be means of exiting nested loops in python?


Raising a dedicated exception can do the trick.

class NestedLoopExit(Exception):
pass

def somefun(aList, aCond, anotherList):
try:
for e in eList:
for e2 in anotherList:
if aCond:
raise NestedLoopExit
else:
do_something_useful()
except NestedLoopExit:
pass
--
Alexandre Fayolle
LOGILAB, Paris (France).
http://www.logilab.com http://www.logilab.fr http://www.logilab.org
Développement logiciel avancé - Intelligence Artificielle - Formations
Jul 18 '05 #2

P: n/a
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.

Jul 18 '05 #3

P: n/a
In article <sl***************@largo.fayauffre.org>,
Alexandre Fayolle <al***************@logilab.fr> wrote:
Dans l'article <bm**********@ocasysi.rubbernet.net>, Oleg Leschov a écrit :

Could there be means of exiting nested loops in python?


Raising a dedicated exception can do the trick.

class NestedLoopExit(Exception):
pass

def somefun(aList, aCond, anotherList):
try:
for e in eList:
for e2 in anotherList:
if aCond:
raise NestedLoopExit
else:
do_something_useful()
except NestedLoopExit:
pass

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.