Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum level of recursion in Python

What's the maximum level of recursion and how do I change it in Python?

like image 581
Tourrent Avatar asked Jul 20 '10 11:07

Tourrent


People also ask

What is the maximum recursion limit?

The recursion limit is usually 1000.

What does maximum recursion depth exceeded mean in Python?

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.

What is the maximum number of levels in a recursion in recursion maximum number of levels present are?

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.


2 Answers

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.

like image 193
Lizard Avatar answered Oct 23 '22 01:10

Lizard


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.

like image 6
ofer.sheffer Avatar answered Oct 23 '22 03:10

ofer.sheffer