27
votes

What's the maximum level of recursion and how do I change it in Python?

2
lol!! loving it! Just like google.co.uk/search?hl=en&q=recursion - Lizard
@Zeus, I tried to close this as a duplicate of itself. Too bad it didn't work. - senderle

2 Answers

31
votes

The default is 1000 levels deep and you can change that using the setrecursionlimit function in the sys module.

Warning:

Beware that some operating systems may start running into problems if you go much higher due to limited stack space.

6
votes

Thought I will add a code example:

import sys
sys.setrecursionlimit(100000)

As Lizard noted, default is 1000 for a reason and the warning is important. Trying a high recursion limit on fibonacci(10000) ( return f(n-1) + f(n-2) ) was enough to shut down my Python IDE. Not getting the 'recursion depth reached' warning did not mean the problem was solved.