Why is the maximum recursion depth in python 1000?
python
Solution
Here is a better test:
n = 0
def test_recursion_limit():
def call():
global n
n += 1
call()
try:
call()
except RuntimeError:
print(n)
test_recursion_limit()
If you put it in `spam.py` and execute that, it should return 998 for both python2 and python3. It's one stack frame short because of the initial `test_recursion_limit` frame.
If you're running in a REPL such as ipython, you are already inside a few frames, so you will see a lower count - it's not that the recursion limit is undershot, it's that the implementation of the REPL itself uses some stack frames.
>>> # freshly opened ipython session
>>> import inspect
>>> len(inspect.stack())
10
You can check the current recursion limit by calling `sys.getrecursionlimit()` function. The default value of 1000 is chosen as a sensible default, it's a safeguard against eclipsing system resources when you accidentally execute an infinitely recursive call. That's very easy to do when mucking around with custom `__getattr__` implementations, for example.
If you're blowing the stack legitimately and you need to increase the limit, it can be modified with `sys.setrecursionlimit`.
Problem
I was curious about what the MRD (maximum recursion depth) is in python, so i wrote this: ``` def call(n): print (n) return call(n+1) call(1) ``` The end result was 979, wich is a peculiar number for me. I could not find anywhere why this number is the standard. As i am a self taught programmer i would apreciate it being explained in simple terms. EDIT: apperantly it's supposed to be a 1000, but why this number?