I understand that sys.setrecursionlimit
works as follows:
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.
Suppose we implement a trivial recursive function and set the recursion limit to 100:
def rec(N):
if N==0:
return 1
else:
return rec(N-1);
sys.setrecursionlimit(100)
If I try rec(99)
(100 recursive calls), I get:
RuntimeError: maximum recursion depth exceeded
To calculate rec(99)
I need to set recursion limit to 105.
Why is this so?
It's poorly named. It should say Stack Depth, not Recursion depth. Recursion implies it's the same thread over and over again that it's limiting. In reality, you could have actual code that just has calls 100 deep. I wouldn't recommend it, but you could. They can get away with it because in the practical world, the only times you ever run into this scenario is with recursion. When you crash because of this, seeing the word "Recursion" gives you an immediate clue of what to look for as opposed to "Stack".
(Stack should give any decent programmer the same clue, but let's be honest, your code just crashed and you want a relevant error message, right? 99.99999% of the time this tells you exactly what you messed up (you missed your base case for recursion.))