infinite loop

Discussion in 'Python' started by LOPEZ GARCIA DE LOMANA, ADRIAN, Sep 6, 2005.

  1. 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.
    LOPEZ GARCIA DE LOMANA, ADRIAN, Sep 6, 2005
    #1
    1. Advertising

  2. LOPEZ GARCIA DE LOMANA, ADRIAN

    Devan L Guest

    LOPEZ GARCIA DE LOMANA, ADRIAN wrote:
    > 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.


    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.
    Devan L, Sep 6, 2005
    #2
    1. Advertising

  3. LOPEZ GARCIA DE LOMANA, ADRIAN wrote:
    > 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?


    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 David Daniels, Sep 6, 2005
    #3
  4. LOPEZ GARCIA DE LOMANA, ADRIAN

    James Guest

    Devan L wrote:
    > LOPEZ GARCIA DE LOMANA, ADRIAN wrote:
    > > 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.

    >
    > 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.


    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.
    James, Sep 6, 2005
    #4
  5. LOPEZ GARCIA DE LOMANA, ADRIAN

    Mike Meyer Guest

    "LOPEZ GARCIA DE LOMANA, ADRIAN" <> writes:

    > 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:


    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 <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Sep 7, 2005
    #5
    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. Vedran Vukotic

    infinite loop unexpectly dies

    Vedran Vukotic, Mar 2, 2006, in forum: Perl
    Replies:
    0
    Views:
    4,450
    Vedran Vukotic
    Mar 2, 2006
  2. Alexander Bosch

    Infinite loop when using Server.Transfer

    Alexander Bosch, Oct 28, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    861
    Steven Cheng[MSFT]
    Nov 10, 2004
  3. Alexander Bosch

    Infinite loop when using Server.Transfer

    Alexander Bosch, Oct 31, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    538
    Steven Cheng[MSFT]
    Nov 1, 2004
  4. Replies:
    5
    Views:
    586
    benben
    Jan 31, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    348
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page