nested loops

Discussion in 'Python' started by Alexandre Fayolle, Oct 9, 2003.

  1. Dans l'article <bm3sar$7e3$>, 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
    Alexandre Fayolle, Oct 9, 2003
    #1
    1. Advertising

  2. Alexandre Fayolle

    anton muhin Guest

    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.
    anton muhin, Oct 9, 2003
    #2
    1. Advertising

  3. Alexandre Fayolle

    Oleg Leschov Guest

    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?
    Oleg Leschov, Oct 9, 2003
    #3
  4. In article <>,
    Alexandre Fayolle <> wrote:
    >Dans l'article <bm3sar$7e3$>, 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 <>
    Business: http://www.Phaseit.net
    Cameron Laird, Oct 10, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Allan Bruce

    dynamic number of nested loops

    Allan Bruce, Jul 1, 2004, in forum: Java
    Replies:
    5
    Views:
    9,759
    Tor Iver Wilhelmsen
    Jul 3, 2004
  2. Porthos
    Replies:
    3
    Views:
    482
    Joris Gillis
    Feb 7, 2005
  3. SplaTTer
    Replies:
    2
    Views:
    377
    SplaTTer
    Jul 2, 2003
  4. viza

    break or continue out of nested loops

    viza, Jul 16, 2003, in forum: C Programming
    Replies:
    5
    Views:
    1,017
    sunil
    Jul 17, 2003
  5. Me
    Replies:
    2
    Views:
    241
Loading...

Share This Page