Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max recursion is not exactly what sys.getrecursionlimit() claims. How come?

I've made a small function that will actually measure the max recursion limit:

def f(x):     r = x     try:         r = f(x+1)     except Exception as e:         print(e)     finally:         return r 

To know what to expect I've checked:

In [28]: import sys  In [29]: sys.getrecursionlimit() Out[29]: 1000 

However

In [30]: f(0) maximum recursion depth exceeded Out[30]: 970 

The number is not fixed, always around ~970, and slightly changes between different instances of python (e.g. from within spyder to system cmd prompt).

Please note that I'm using ipython on python3.

What's going on? Why is the actual limit I'm getting lower than the sys.getrecursionlimit() value?

like image 391
Aguy Avatar asked Jul 08 '16 11:07

Aguy


People also ask

How do you fix maximum recursion depth exceeded?

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.

How do I fix maximum recursion depth exceeded in comparison Python?

The recursion depth limit in Python is by default 1000 . You can change it using sys. setrecursionlimit() function.

How does Python handle maximum recursion depth?

The Python interpreter limits the recursion limit so that infinite recursions are avoided. The “sys” module in Python provides a function called setrecursionlimit() to modify the recursion limit in Python. It takes one parameter, the value of the new recursion limit. By default, this value is usually 10^3.

What is the limit of recursion?

Python's default recursion limit is 1000, meaning that Python won't let a function call on itself more than 1000 times, which for most people is probably enough. The limit exists because allowing recursion to occur more than 1000 times doesn't exactly make for lightweight code.


2 Answers

The recursion limit is not the limit on recursion but the maximum depth of the python interpreter stack.There is something on the stack before your function gets executed. Spyder executes some python stuff before it calls your script, as do other interpreters like ipython.

You can inspect the stack via methods in the inspect module.

In CPython for me:

>>>print(len(inspect.stack())) 1 

In Ipython for me:

>>>print(len(inspect.stack())) 10 

As knbk pointed out in the comments as soon as you hit the stack limit a RecursionError is thrown and the interpreter raises the stack limit a bit to give you a possibility to handle the error gracefully. If you also exhaust that limit python will crash.

like image 62
syntonym Avatar answered Sep 25 '22 15:09

syntonym


This limit is for stack, not for the function you define. There are other internal things which might push something to stack.

And of course it could depend on env in which it was executed. Some can pollute stack more, some less.

like image 31
Viacheslav Kondratiuk Avatar answered Sep 25 '22 15:09

Viacheslav Kondratiuk