infinite loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LOPEZ GARCIA DE LOMANA, ADRIAN

    #1

    infinite loop


    Hi all,

    I have a question with some code I'm writting:


    def main():

    if option == 1:

    function_a()

    elif option == 2:

    function_b()

    else:

    raise 'option has to be either 1 or 2'

    if iteration == True:

    main()

    def function_a():

    print 'hello from function a'

    return None

    def function_b():

    print 'hello from function b'

    return None

    iteration = True

    option = 1

    main()


    I want an infinite loop, but after some iterations (996) it breaks:


    [alopez@dhcp-222 tmp]$ python test.py
    hello from function a
    hello from function a
    hello from function a
    ..
    ..
    ..
    hello from function a
    hello from function a
    Traceback (most recent call last):
    File "test.py", line 35, in ?
    main()
    File "test.py", line 17, in main
    main()
    File "test.py", line 17, in main

    ..
    ..
    ..
    ..
    File "test.py", line 17, in main
    main()
    File "test.py", line 17, in main
    main()
    File "test.py", line 5, in main
    function_a()
    RuntimeError: maximum recursion depth exceeded


    I don't understand it. Why am I not allowed to iterate infinitely? Something about the functions? What should I do for having an infinite loop?

    Thanks in advance for your help,

    Adrián.


  • Devan L

    #2
    Re: infinite loop


    LOPEZ GARCIA DE LOMANA, ADRIAN wrote:[color=blue]
    > Hi all,
    >
    > I have a question with some code I'm writting:
    >
    >
    > def main():
    >
    > if option == 1:
    >
    > function_a()
    >
    > elif option == 2:
    >
    > function_b()
    >
    > else:
    >
    > raise 'option has to be either 1 or 2'
    >
    > if iteration == True:
    >
    > main()
    >
    > def function_a():
    >
    > print 'hello from function a'
    >
    > return None
    >
    > def function_b():
    >
    > print 'hello from function b'
    >
    > return None
    >
    > iteration = True
    >
    > option = 1
    >
    > main()
    >
    >
    > I want an infinite loop, but after some iterations (996) it breaks:
    >
    >
    > [alopez@dhcp-222 tmp]$ python test.py
    > hello from function a
    > hello from function a
    > hello from function a
    > .
    > .
    > .
    > hello from function a
    > hello from function a
    > Traceback (most recent call last):
    > File "test.py", line 35, in ?
    > main()
    > File "test.py", line 17, in main
    > main()
    > File "test.py", line 17, in main
    >
    > .
    > .
    > .
    > .
    > File "test.py", line 17, in main
    > main()
    > File "test.py", line 17, in main
    > main()
    > File "test.py", line 5, in main
    > function_a()
    > RuntimeError: maximum recursion depth exceeded
    >
    >
    > I don't understand it. Why am I not allowed to iterate infinitely? Something about the functions? What should I do for having an infinite loop?
    >
    > Thanks in advance for your help,
    >
    > Adrián.[/color]

    You've written a recursive function-you're not iterating. The recursion
    limit is there to keep you from making something which will do
    something bad, like recurse cyclically.

    Comment

    • Scott David Daniels

      #3
      Re: infinite loop

      LOPEZ GARCIA DE LOMANA, ADRIAN wrote:[color=blue]
      > Hi all,
      >
      > I have a question with some code I'm writting:
      >
      >
      > def main():
      > if option == 1:
      > function_a()
      > elif option == 2:
      > function_b()
      > else:
      > raise 'option has to be either 1 or 2'
      > if iteration == True:
      > main()
      > ... I want an infinite loop, but after some iterations (996) it breaks:
      > ... RuntimeError: maximum recursion depth exceeded
      >
      >
      > I don't understand it. Why am I not allowed to iterate infinitely?
      > Something about the functions? What should I do for having an infinite loop?[/color]

      You are asking in your code for infinite recursive regress.
      Eventually the stack overflows.

      An infinite loop would look like:

      def main():
      if option == 1:
      function_a()
      elif option == 2:
      function_b()
      else:
      raise 'option has to be either 1 or 2'
      while iteration:
      if option == 1:
      function_a()
      elif option == 2:
      function_b()
      else:
      raise 'option has to be either 1 or 2'

      Which you might want to rewrite as:
      def main():
      choices = {1: function_a, 2:function_b}
      choices[option]()
      while iteration:
      choices[option]()

      --Scott David Daniels
      Scott.Daniels@A cm.Org

      Comment

      • James

        #4
        Re: infinite loop

        Devan L wrote:[color=blue]
        > LOPEZ GARCIA DE LOMANA, ADRIAN wrote:[color=green]
        > > Hi all,
        > >
        > > I have a question with some code I'm writting:
        > >
        > >
        > > def main():
        > >
        > > if option == 1:
        > >
        > > function_a()
        > >
        > > elif option == 2:
        > >
        > > function_b()
        > >
        > > else:
        > >
        > > raise 'option has to be either 1 or 2'
        > >
        > > if iteration == True:
        > >
        > > main()
        > >
        > > def function_a():
        > >
        > > print 'hello from function a'
        > >
        > > return None
        > >
        > > def function_b():
        > >
        > > print 'hello from function b'
        > >
        > > return None
        > >
        > > iteration = True
        > >
        > > option = 1
        > >
        > > main()
        > >
        > >
        > > I want an infinite loop, but after some iterations (996) it breaks:
        > >
        > >
        > > [alopez@dhcp-222 tmp]$ python test.py
        > > hello from function a
        > > hello from function a
        > > hello from function a
        > > .
        > > .
        > > .
        > > hello from function a
        > > hello from function a
        > > Traceback (most recent call last):
        > > File "test.py", line 35, in ?
        > > main()
        > > File "test.py", line 17, in main
        > > main()
        > > File "test.py", line 17, in main
        > >
        > > .
        > > .
        > > .
        > > .
        > > File "test.py", line 17, in main
        > > main()
        > > File "test.py", line 17, in main
        > > main()
        > > File "test.py", line 5, in main
        > > function_a()
        > > RuntimeError: maximum recursion depth exceeded
        > >
        > >
        > > I don't understand it. Why am I not allowed to iterate infinitely? Something about the functions? What should I do for having an infinite loop?
        > >
        > > Thanks in advance for your help,
        > >
        > > Adrián.[/color]
        >
        > You've written a recursive function-you're not iterating. The recursion
        > limit is there to keep you from making something which will do
        > something bad, like recurse cyclically.[/color]

        What you need is probably this...

        def main():
        while iteration:
        if option == 1:
        function_a()
        elif option == 2:
        function_b()
        else:
        raise 'option has to be either 1 or 2'

        def function_a():
        print 'hello from function a'

        def function_b():
        print 'hello from function b'

        iteration = True
        option = 1
        main()

        As a side note, note that you don't really need to return a None.

        Comment

        • Mike Meyer

          #5
          Re: infinite loop

          "LOPEZ GARCIA DE LOMANA, ADRIAN" <alopez@imim.es > writes:
          [color=blue]
          > Hi all,
          >
          > I have a question with some code I'm writting:
          >
          >
          > def main():
          > if option == 1:
          > function_a()
          > elif option == 2:
          > function_b()
          > else:
          > raise 'option has to be either 1 or 2'
          > if iteration == True:
          > main()[/color]
          [...][color=blue]
          > I want an infinite loop, but after some iterations (996) it breaks:[/color]

          Since no one else mentioend it: this is only iteration in languages
          which mandate tail recursion elimination. Languages that don't do that
          are free to do the recursion, which will eventually run you out of
          stack. Python is in the latter category, and that's what you ran into.

          Thinking about iteration this way is elegant - but it doesn't work
          everywhere. Sorry.

          <mike
          --
          Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
          Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

          Comment

          Working...