RuntimeError 'maximum recursion depth exceeded'

G

Georgy Pruss

Sometimes I get this error.
E.g.
............
RuntimeError: maximum recursion depth exceeded

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

G-:
 
R

Ray Smith

G

Georgy Pruss

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

| Georgy Pruss wrote:
|
| > RuntimeError: maximum recursion depth exceeded
| >
| > Is there any way to set a bigger stack in Python?
|
|
| Yep: sys.setrecursionlimit
|
| --Irmen
|
 
I

Irmen de Jong

Georgy said:
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
 
G

Georgy Pruss

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


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

Gerrit Holl

Georgy said:
Sometimes I get this error.
E.g.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top