469,266 Members | 1,679 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,266 developers. It's quick & easy.

RuntimeError 'maximum recursion depth exceeded'

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('base6 4')
Jul 18 '05 #1
6 44395
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=e...mp.lang.python

would have found the answer.

Regards,

Ray Smith

Jul 18 '05 #2
Georgy Pruss wrote:
RuntimeError: maximum recursion depth exceeded

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

Yep: sys.setrecursionlimit

--Irmen

Jul 18 '05 #3
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:3f***********************@news.xs4all.nl...
| Georgy Pruss wrote:
|
| > RuntimeError: maximum recursion depth exceeded
| >
| > Is there any way to set a bigger stack in Python?
|
|
| Yep: sys.setrecursionlimit
|
| --Irmen
|
Jul 18 '05 #4
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

Jul 18 '05 #5
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:3f***********************@news.xs4all.nl...
| 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
|
Jul 18 '05 #6
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/...-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/

Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Bill Loren | last post: by
2 posts views Thread by Sujit Marar | last post: by
5 posts views Thread by yogee | last post: by
14 posts views Thread by olekristianvillabo | last post: by
10 posts views Thread by MakeMineScotch | last post: by
reply views Thread by cwig | last post: by
reply views Thread by jobs | last post: by
6 posts views Thread by globalrev | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.