nested loops

Alexandre Fayolle alf at calvin.fayauffre.org
Thu Oct 9 04:19:59 EDT 2003


Dans l'article <bm3sar$7e3$4 at 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




More information about the Python-list mailing list