RuntimeError 'maximum recursion depth exceeded'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Georgy Pruss

    RuntimeError 'maximum recursion depth exceeded'

    Sometimes I get this error.
    E.g.
    [color=blue][color=green][color=darkred]
    >>> sum = lambda n: n<=1 or n+sum(n-1) # just to illustrate the error
    >>> sum(999)[/color][/color][/color]
    499500[color=blue][color=green][color=darkred]
    >>> sum(1000)[/color][/color][/color]
    ............
    RuntimeError: maximum recursion depth exceeded

    Is there any way to set a bigger stack in Python?

    G-:
    --
    Georgy Pruss
    E^mail: 'ZDAwMTEyMHQwMz MwQGhvdG1haWwuY 29t\n'.decode(' base64')


  • Ray Smith

    #2
    Re: RuntimeError 'maximum recursion depth exceeded'

    Georgy Pruss wrote:[color=blue]
    > Sometimes I get this error.
    > E.g.
    >
    >[color=green][color=darkred]
    >>>>sum = lambda n: n<=1 or n+sum(n-1) # just to illustrate the error
    >>>>sum(999)[/color][/color]
    >
    > 499500
    >[color=green][color=darkred]
    >>>>sum(1000)[/color][/color]
    >
    > ...........
    > RuntimeError: maximum recursion depth exceeded
    >
    > Is there any way to set a bigger stack in Python?
    >
    > G-:[/color]

    See

    sys.getrecursio nlimit(), and
    sys.setrecursio nlimit(limit)

    doco at:

    The official home of the Python Programming Language


    a quick search of the doco or the newsgroup archive at:


    would have found the answer.

    Regards,

    Ray Smith

    Comment

    • Irmen de Jong

      #3
      Re: RuntimeError 'maximum recursion depth exceeded'

      Georgy Pruss wrote:
      [color=blue]
      > RuntimeError: maximum recursion depth exceeded
      >
      > Is there any way to set a bigger stack in Python?[/color]


      Yep: sys.setrecursio nlimit

      --Irmen

      Comment

      • Georgy Pruss

        #4
        Re: RuntimeError 'maximum recursion depth exceeded'

        Thank you.
        Now it's "MemoryErro r: Stack overflow" but it's another story. :)
        G-:

        "Irmen de Jong" <irmen@-NOSPAM-REMOVETHIS-xs4all.nl> wrote in message news:3fb617c0$0 $58715$e4fe514c @news.xs4all.nl ...
        | Georgy Pruss wrote:
        |
        | > RuntimeError: maximum recursion depth exceeded
        | >
        | > Is there any way to set a bigger stack in Python?
        |
        |
        | Yep: sys.setrecursio nlimit
        |
        | --Irmen
        |


        Comment

        • Irmen de Jong

          #5
          Re: RuntimeError 'maximum recursion depth exceeded'

          Georgy Pruss wrote:[color=blue]
          > Thank you.
          > Now it's "MemoryErro r: Stack overflow" but it's another story. :)[/color]

          Hmm, what are you doing exactly that requires this deep recursion?
          Can't you rewrite your algorithm in a non-recursive way?

          --Irmen

          Comment

          • Georgy Pruss

            #6
            Re: RuntimeError 'maximum recursion depth exceeded'

            No, I'm not complaining. It's good that Python doesn't just freeze or crash.
            The algorithm is "deep first" walk in a maze, so for 100x100 mazes the
            recursion is well deeper than 1000 levels and the stack is not big enough indeed.
            Of course, for big dimentions the algorithm needs to be re-written w/o recursion.

            Georgy Pruss
            --
            p='p=;print p[:2]+chr(39)+p+chr( 39)+p[2:]';print p[:2]+chr(39)+p+chr( 39)+p[2:]


            "Irmen de Jong" <irmen@-NOSPAM-REMOVETHIS-xs4all.nl> wrote in message news:3fb6b5bb$0 $58714$e4fe514c @news.xs4all.nl ...
            | Georgy Pruss wrote:
            | > Thank you.
            | > Now it's "MemoryErro r: Stack overflow" but it's another story. :)
            |
            | Hmm, what are you doing exactly that requires this deep recursion?
            | Can't you rewrite your algorithm in a non-recursive way?
            |
            | --Irmen
            |


            Comment

            • Gerrit Holl

              #7
              Re: RuntimeError 'maximum recursion depth exceeded'

              Georgy Pruss wrote:[color=blue]
              > Sometimes I get this error.
              > E.g.
              >[color=green][color=darkred]
              > >>> sum = lambda n: n<=1 or n+sum(n-1) # just to illustrate the error
              > >>> sum(999)[/color][/color]
              > 499500[color=green][color=darkred]
              > >>> sum(1000)[/color][/color]
              > ...........
              > RuntimeError: maximum recursion depth exceeded
              >
              > Is there any way to set a bigger stack in Python?[/color]

              Yes, use sys.setrecursio nlimit()
              See http://www.python.org/dev/doc/devel/lib/module-sys.html :

              setrecursionlim it(limit)
              Set the maximum depth of the Python interpreter stack to limit. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python.

              The highest possible limit is platform-dependent. A user may need to set the limit higher when she has a program that requires deep recursion and a platform that supports a higher limit. This should be done with care, because a too-high limit can lead to a crash.

              Note that in this case, you can simlpy use the builtin sum:
              See http://www.python.org/dev/doc/devel/...-in-funcs.html :

              sum(sequence[, start])
              Sums start and the items of a sequence, from left to right, and returns the total. start defaults to 0. The sequence's items are normally numbers, and are not allowed to be strings. The fast, correct way to concatenate sequence of strings is by calling ''.join(sequenc e). Note that sum(range(n), m) is equivalent to reduce(operator .add, range(n), m) New in version 2.3.

              ....but I assume you already knew the latter, since you said that you example
              was just to illustrate the error. Just to be sure.

              yours,
              Gerrit.

              --
              This space intentionally left blank.
              --
              Asperger Syndroom - een persoonlijke benadering:

              Kom in verzet tegen dit kabinet:
              De website van de Socialistische Partij (SP) in Nederland: Informatie, nieuws, agenda en publicaties.


              Comment

              Working...