Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RuntimeError 'maximum recursion depth exceeded'

4 views
Skip to first unread message

Georgy Pruss

unread,
Nov 15, 2003, 5:14:23 AM11/15/03
to
Sometimes I get this error.
E.g.

>>> sum = lambda n: n<=1 or n+sum(n-1) # just to illustrate the error
>>> sum(999)
499500
>>> sum(1000)
...........
RuntimeError: maximum recursion depth exceeded

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

G-:
--
Georgy Pruss
E^mail: 'ZDAwMTEyMHQwMzMwQGhvdG1haWwuY29t\n'.decode('base64')


Ray Smith

unread,
Nov 15, 2003, 7:08:32 AM11/15/03
to
Georgy Pruss wrote:
> Sometimes I get this error.
> E.g.
>
>
>>>>sum = lambda n: n<=1 or n+sum(n-1) # just to illustrate the error
>>>>sum(999)
>
> 499500
>
>>>>sum(1000)
>
> ...........
> RuntimeError: maximum recursion depth exceeded
>
> Is there any way to set a bigger stack in Python?
>
> G-:

See

sys.getrecursionlimit(), and
sys.setrecursionlimit(limit)

doco at:

http://python.org/doc/current/lib/module-sys.html

a quick search of the doco or the newsgroup archive at:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&group=comp.lang.python

would have found the answer.

Regards,

Ray Smith

Irmen de Jong

unread,
Nov 15, 2003, 7:10:42 AM11/15/03
to
Georgy Pruss wrote:

> RuntimeError: maximum recursion depth exceeded
>
> Is there any way to set a bigger stack in Python?


Yep: sys.setrecursionlimit

--Irmen

Georgy Pruss

unread,
Nov 15, 2003, 5:53:24 PM11/15/03
to
Thank you.
Now it's "MemoryError: Stack overflow" but it's another story. :)
G-:

"Irmen de Jong" <irmen@-NOSPAM-REMOVETHIS-xs4all.nl> wrote in message news:3fb617c0$0$58715$e4fe...@news.xs4all.nl...

Irmen de Jong

unread,
Nov 15, 2003, 6:24:43 PM11/15/03
to
Georgy Pruss wrote:
> Thank you.
> Now it's "MemoryError: 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

Georgy Pruss

unread,
Nov 15, 2003, 8:26:29 PM11/15/03
to
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$e4fe...@news.xs4all.nl...

Gerrit Holl

unread,
Nov 16, 2003, 4:28:21 AM11/16/03
to Georgy Pruss, pytho...@python.org
Georgy Pruss wrote:
> Sometimes I get this error.
> E.g.
>
> >>> sum = lambda n: n<=1 or n+sum(n-1) # just to illustrate the error
> >>> sum(999)
> 499500
> >>> sum(1000)
> ...........
> RuntimeError: maximum recursion depth exceeded
>
> Is there any way to set a bigger stack in Python?

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

setrecursionlimit(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/lib/built-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(sequence). 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:
http://people.nl.linux.org/~gerrit/
Kom in verzet tegen dit kabinet:
http://www.sp.nl/

0 new messages