nested loops
Cameron Laird
claird at lairds.com
Fri Oct 10 14:59:56 EDT 2003
In article <slrnboa6hf.kf.alf at largo.fayauffre.org>,
Alexandre Fayolle <alexandre.fayolle at logilab.fr> wrote:
>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
.
.
.
As some of us are working in other-than-our-native languages, I
want to reinforce Mr. Fayolle's observation explicitly: use of
such an exception is not only an alternative to the "labelled
break" Mr. Leschov originally sought, it's the one I consider
most idiomatic in Python. Exceptions are important in Python;
make their acquaintance.
--
Cameron Laird <claird at phaseit.net>
Business: http://www.Phaseit.net
More information about the Python-list
mailing list