>>> 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')
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
> RuntimeError: maximum recursion depth exceeded
>
> Is there any way to set a bigger stack in Python?
Yep: sys.setrecursionlimit
--Irmen
"Irmen de Jong" <irmen@-NOSPAM-REMOVETHIS-xs4all.nl> wrote in message news:3fb617c0$0$58715$e4fe...@news.xs4all.nl...
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
--
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...
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/