# Python maximum recursion depth exceeded in comparison

Before jumping into an error, maximum recursion depth exceeded in comparison. Let’s first understand the basics of recursion and how recursion works in Python.

## What is Recursion?

Recursion in computer language is a process in which a function calls itself directly or indirectly, and the corresponding function is called a recursive function.

## A classic example of recursion

The most classic example of recursive programming everyone would have learned the factorial of a number. Factorial of a number is the product of all positive integers less than or equal to a given positive integer.

For example, factorial(5) is 5*4*3*2*1, and factorial(3) is 3*2*1.

Similarly, you can use recursive in many other scenarios like the Fibonacci seriesTower of HanoiTree TraversalsDFS of Graph, etc.

## Why does Python throw maximum recursion depth exceeded in comparison?

As we already know, recursive functions call by itself directly or indirectly, and during this process, the execution should go on infinitely.

Python limits the number of times a recursive function can call by itself to ensure it does not execute infinitely and cause a stack overflow error.

## How to check maximum recursion depth in Python?

You can check the maximum recursion depth in Python using the code sys.getrecursionlimit(). Python doesn’t have excellent support for recursion because of its lack of TRE (Tail Recursion Elimination). By default, the recursion limit set in Python is 1000.

``````def fibonacci(n):
if n <= 1:
return n
else:
return(fibonacci(n-1) + fibonacci(n-2))
print(fibonacci(1500))

#Output RecursionError: maximum recursion depth exceeded in comparison
``````

## How do you fix the Recursionerror maximum recursion depth exceeded while calling a Python Object?

Let’s write a recursive function to calculate the Fibonacci series for a given number.

Since you are finding a Fibonacci of 1500 and the default recursion limit in Python is 1000, you will get an error stating “RecursionError: maximum recursion depth exceeded in comparison.”

This can be fixed by increasing the recursion limit in Python, below is the snippet on how you can increase the recursion limit.

``````import sys
sys.setrecursionlimit(1500)``````

## Closing thoughts

This code sets the maximum recursion depth to 1500, and you could even change this to a higher limit. However, it is not recommended to perform this operation as the default limit is mostly good enough, and Python isn’t a functional language, and tail recursion is not a particularly efficient technique. Rewriting the algorithm iteratively, if possible, is generally a better idea.

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

## Python String find()

Table of Contents Hide SyntaxParametersReturn ValueDifference between find() method and index() methodExample 1: find() method without any argumentsExample 2: find() method with start and end Arguments The Python String find()…

## TypeError: ‘NoneType’ object is not iterable

Table of Contents Hide Explanation of TypeError : ‘NoneType’ object is not iterableIterating over a variable that has value None fails:Python methods return NoneType if they don’t return a value:Concatenation…

## How to get hostname in Python?

Table of Contents Hide Python code to get a hostname using the socket modulePython code to find a hostname using the platform modulePython code to find a hostname using the…

## Python String isdecimal()

Table of Contents Hide SyntaxParametersReturn ValueExample 1: Working of isdecimal() method in PythonExample 2: String Containing digits and Numeric Characters The Python String isdecimal() method is a built-in function that…

## numpy.mean() Function

Table of Contents Hide SyntaxParametersReturn ValueExample 1: Using the numpy.mean() method on a flattened arrayExample 2: Compute the mean of the array column-wise using numpy.mean()Example 3: Compute the mean of…