What's the maximum level of recursion and how do I change it in Python?
The recursion limit is usually 1000.
The “maximum recursion depth exceeded in comparison” error is raised when you try to execute a function that exceeds Python's built in recursion limit. You can fix this error by rewriting your program to use an iterative approach or by increasing the recursion limit in Python.
2 Answers. Show activity on this post. The default is 1000 levels deep and you can change that using the setrecursionlimit function in the sys module.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With