nested loops

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Oleg Leschov

    nested loops


    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?


  • Alexandre Fayolle

    #2
    Re: nested loops

    Dans l'article <bm3sar$7e3$4@o casysi.rubberne t.net>, Oleg Leschov a écrit :[color=blue]
    >
    > Could there be means of exiting nested loops in python?[/color]

    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_us eful()
    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

    Comment

    • anton muhin

      #3
      Re: nested loops

      Oleg Leschov wrote:
      [color=blue]
      > 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?
      >
      >[/color]
      Not an ideal solution, but still might be of interest:
      [color=blue][color=green][color=darkred]
      >>> for x in range(100):[/color][/color][/color]
      .... 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.

      Comment

      • Cameron Laird

        #4
        Re: nested loops

        In article <slrnboa6hf.kf. alf@largo.fayau ffre.org>,
        Alexandre Fayolle <alexandre.fayo lle@logilab.fr> wrote:[color=blue]
        >Dans l'article <bm3sar$7e3$4@o casysi.rubberne t.net>, Oleg Leschov a écrit :[color=green]
        >>
        >> Could there be means of exiting nested loops in python?[/color]
        >
        >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_us eful()
        > except NestedLoopExit:
        > pass[/color]

        Comment

        Working...