What is The Maximum Recursion Depth In Python, And How To Increase?

In this tutorial, we will learn What is the maximum recursion depth in Python, and how to increase it as we have a limited depth of recursion in python which is 1000 times only but we can always increase it as per our requirements, and we will learn here how we can increase them by using some built in functions.



Recursion is nothing but the calling of the same function time and again. It is used as the loop until the condition is not false. There might be a situation when the recursion may keep on accruing and that could be in two situations when either the increament/ decrements basically updation of variable is not done or the return type is not being encountered.

And not encountering of return type may also have two conditions either the return is not given or it is given but at some place where the condition would never be true and it would not be executed any time. It is an alternative loop that we can use.

And keep on the calling function in case of recursion may lead to the killing of the memory and may occupy a lot of memory in case of by default it may at most 1000 times and after that, it will show the error of stack overflow so we can not go beyond that.

The most important thing here is we can increase the number of recursion as per your choice by using the code example given below:

import sys

sys.setrecursionlimit(5000) # Set new recursion depth limit to 5000

def my_ function(n):
    if n == 0:
        return 0
        return my_ function(n-1)

result = my_ function(4000) # This would raise RecursionError if recursion depth is still 1000

print( result)

By the above code we can increase it to 5000 times so here we had increased it by using an in built function sys. setrecursionlimit() of the module sys which we can use by simply importing from the library.


To learn more What is the maximum recursion depth in Python, and how to increase it visit: Recursion Functions in Python and enhance the limit of execution.

To learn more about the solutions to the problems faced in python and concepts related to programming languages along with tutorials and solutions visit: Python Tutorials And Problems.

To learn more about different other programming languages, their concepts, and tutorials on the solutions to the problems faced in the programming visit: beta python programming languages Solutions

Leave a Comment

%d bloggers like this: