Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > nested loops

Reply
Thread Tools

nested loops

 
 
Alexandre Fayolle
Guest
Posts: n/a
 
      10-09-2003
Dans l'article <bm3sar$7e3$(E-Mail Removed)>, 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
 
Reply With Quote
 
 
 
 
anton muhin
Guest
Posts: n/a
 
      10-09-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.

 
Reply With Quote
 
 
 
 
Oleg Leschov
Guest
Posts: n/a
 
      10-09-2003

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?


 
Reply With Quote
 
Cameron Laird
Guest
Posts: n/a
 
      10-10-2003
In article <(E-Mail Removed)>,
Alexandre Fayolle <(E-Mail Removed)> wrote:
>Dans l'article <bm3sar$7e3$(E-Mail Removed)>, 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 <(E-Mail Removed)>
Business: http://www.Phaseit.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
nested loops and conditional statements Porthos XML 3 02-07-2005 08:47 PM
dynamic number of nested loops Allan Bruce Java 5 07-03-2004 09:12 PM
break or continue out of nested loops viza C Programming 5 07-17-2003 04:04 AM
compacting similar nested for loops SplaTTer C++ 2 07-02-2003 09:16 PM



Advertisments