# [Solved] RecursionError: maximum recursion depth exceeded while calling a Python object

A Recursive function in programming is a function which calls itself. These functions find applications while constructing programs for factorial, Fibonacci series, Armstrong numbers, etc. The main idea is to break larger programs into smaller, less complex problems. With recursive functions, generating sequences becomes easy. But while using recursive functions, recursionerror may occur in python. In this article, we shall be looking into one such recursionerror: maximum recursion depth exceeded while calling a Python object

Contents

## What is recursionerror?

As the name suggests, Recursionerror may occur when we are dealing with recursive functions. When we run the recursion function for a large number of times, recursion error is thrown. Python has a limit on the number of times a recursive function can call itself. This is done to ensure that the function does not execute infinitely and stops after some number of iterations. To know the recursion limit in python, we use the following code:

```import sys
print(sys.getrecursionlimit())
```

The output is:

`1000`

## RecursionError: Maximum Recursion Depth Exceeded while calling a Python Object

Let us look at an example of RecursionError: maximum recursion depth exceeded. We shall take an example of a factorial function.

The following code shall generate factorial for a given number.

```def find_fact(n):
if n == 0 or n == 1:
return 1
else :
return (n*find_fact(n-1))

print("Factorial is :", find_fact(5))
```

Here, this program shall be executed successfully and shall print the below output:

`Factorial is : 120`

But if we pass a larger number into the find_fact() function, it will throw RecursionError: Maximum Recursion Depth Exceeded error.

```print("Factorial is :", find_fact(5000))
```

Output:

`RecursionError: maximum recursion depth exceeded in comparison`

Since the recursion function exceeded the limit of 1000 iterations, recursionerror is thrown.

The RecursionError: Maximum Recursion Depth Exceeded error may also be thrown while we are trying to create a nested list whose length exceeds the recursion limit.

Let us take the following example. We have created a function named nested() which accepts one argument – n. Depending on the value of n, the length of that nested list would be created. Let us try to pass a value n greater than the recursion limit.

```def nested(n):
list1 = list2 = []
for i in range(n):
list1.append([])
list1 = list1[0]
return list2

nestedlist = nested(2000)
print(nestedlist)
```

The output will be a recursion error.

`RecursionError: maximum recursion depth exceeded while getting the repr of an object`

## RecursionError: Maximum Recursion Depth Exceeded While Calling A Python Object

The recursionerror for Maximum Recursion Depth Exceeded While Calling A Python Object is thrown when we are trying to call a python object in Django. The error may also occur while using Flask.

When the interpreter detects that the maximum depth for recursion has reached, it throws the recursionerror. To prevent the stack from getting overflow, python raises the recursionerror.

## Best practices to avoid RecursionError: Maximum Recursion Depth Exceeded while calling a Python Object

### 1. Using other loops instead of recursion

To prevent the error from occurring, we can simply convert the piece of code from recursion to a loop statement.

If we take the example of the factorial function, we can convert it into a non – recursive function. We do that by placing a for loop inside the recursion function. The for loop will execute for a length equal to the value of the number.

```def find_fact(n):
mul = 1
for i in range(2,n+1):
mul = mul * i
return mul

print("Factorial is :", find_fact(1500))
```

Now, it will not throw any recursion error and simply print the large factorial number.

### 2. Using sys.setrecursionlimit() function

Else, if we still want to use the recursion function, we can increase the recursion limit from 1000 to a higher number. For that, we have to first import the sys library. Using the sys library, we will use the sys.setrecursionlimit() function.

```import sys
sys.setrecursionlimit(2000)
```

Now, it will not thrown the recursionerror and the program will be executed for larger amount of recursions. On executing the recursive function, it will not throw any error and print its output.

```def find_fact(n):
if n == 0 or n == 1:
return 1
else :
return (n*find_fact(n-1))

print("Factorial is :", find_fact(1500))
```

### 3. Setting boundary conditions

It is necessary to set boundary conditions to ensures that the recursive function comes to an end. In the factorial program, the condition :

`'if n == 1 or n == 0 : return 1'`

is the boundary condition. It is with this condition that the loop comes to an end.

### 4. Creating a converging recursion

While writing the recursion condition, one has to ensure that the condition does come to an end and does not continue infinitely. The recursive calls should eventually tend towards the boundary condition.

We have to ensure that we creating a converging condition for that. In the factorial program, the ‘n*fact(n-1)’ is a converging condition that converges the value from n to 1.

### 5. Using Memoization

We can also use memoization to reduce the computing time of already calculated values. This way, we can speed up the calculations by remembering past calculations.

When recursive calls are made, then with memoization we can store the previously calculated values instead of unnecessarily calculating them again.

That sums up the article on RecursionError: Maximum Recursion Depth Exceeded While Calling A Python Object. If you have any questions in your mind, don’t forget to let us know in the comments below.

Until next time, Keep Learning!

Subscribe
Notify of