4

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?

Zaizer zazza
  • 91
  • 10
  • 3
    Duplicate: http://stackoverflow.com/questions/3323001/maximum-recursion-depth - Read this, it gives you the answer you want.. – Fusseldieb Oct 18 '16 at 18:40
  • 2
    He ask why its there, and i am asking why it is like that. I want to know why my result was 979, **not** why there is a MRD or how to get around it. – Zaizer zazza Oct 18 '16 at 18:43
  • Have you read the [docs](https://docs.python.org/2/library/sys.html#sys.getrecursionlimit)? – UnholySheep Oct 18 '16 at 18:47
  • I also tried it in the shell now, still stop at 979 for me. Docs does not give any information on the standard recursion limit. – Zaizer zazza Oct 18 '16 at 18:54
  • 1
    Because there isn't a real "standard recursion limit" - it can be set by the implementation (and for CPython, which most people refer to as just Python, it is set to 1000 by default). Also it can be changed (which was originally part of your question) as the documentation describes – UnholySheep Oct 18 '16 at 18:58
  • Why is the default 1000? why not 1'000'000, why not 10 or 1024? – Zaizer zazza Oct 18 '16 at 19:05
  • Because the programmers that implemented CPython decided (probably based on their expertise and experience) that it is a good default value that will be sufficient for most programs written in Python – UnholySheep Oct 18 '16 at 19:06
  • Why is 1000 a good default value that will be sufficient for most programs written in Python? – Zaizer zazza Oct 18 '16 at 19:11
  • Which part of "the programmers that implemented CPython decided (probably based on their expertise and experience)" was unclear to you? If you really want to know ask them. I neither wrote CPython nor decided the default value. – UnholySheep Oct 18 '16 at 19:16
  • 5
    `RuntimeError: maximum "why question" depth exceeded` – wim Oct 18 '16 at 19:28

1 Answers1

5

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.

wim
  • 338,267
  • 99
  • 616
  • 750