[Tutor] recursion depth
Emile van Sebille
emile at fenx.com
Wed Jan 8 21:42:45 CET 2014
On 1/8/2014 12:25 PM, Keith Winston wrote:
> I've been playing with recursion, it's very satisfying.
>
> However, it appears that even if I sys.setrecursionlimit(100000), it
> blows up at about 24,000 (appears to reset IDLE). I guess there must be
> a lot of overhead with recursion, if only 24k times are killing my memory?
Yes -- the docs warn specifically about that:
sys.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.
> I'm playing with a challenge a friend gave me: add each number, up to
> 1000, with it's reverse, continuing the process until you've got a
> palindrome number. Then report back the number of iterations it takes.
> There's one number, 196, that never returns, so I skip it. It's a
> perfect place to practice recursion (I do it in the adding part, and the
> palindrome checking part), but apparently I can't help but blow up my
> machine...
Without seeing your code it's hard to be specific, but it's obvious
you'll need to rethink your approach. :)
Emile
More information about the Tutor
mailing list