# 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.

##### You May Also Like ## Python dir()

Table of Contents Hide dir() Syntax dir() Parametersdir() Return ValueExample 1: How dir() works?Example 2: When no parameters are passed to dir() method with and without importing external libraries.Example 3: When a module… ## [Solved] NumPy.ndarray object is Not Callable Python

Table of Contents Hide NumPy.ndarray object is Not Callable ErrorAn ExampleSolution NumPy.ndarray object is Not Callable ErrorConclusion In Python, the array will be accessed using an indexing method. Similarly, the… ## [Solved] RuntimeWarning: invalid value encountered in double_scalars

The RuntimeWarning: invalid value encountered in double_scalars occurs when you perform a complex mathematical operation using NumPy that involves extremely small or very large numbers and also if we pass an invalid… ## Solving environment: failed with initial frozen solve. retrying with flexible solve

Table of Contents Hide What is Solving environment: failed with initial frozen solve. retrying with flexible solve?How to fix Solving environment: failed with initial frozen solve. retrying with flexible solve?Solution… ## TypeError: only integer scalar arrays can be converted to a scalar index

Python numpy throws typeerror: only integer scalar arrays can be converted to a scalar index when you try to convert the ordinary array into a scalar index. The other cause… ## Python String isalpha()

Python string isalpha() method is mainly used to check if the string is the alphabet or not. The isalpha() method returns true if all the characters in the string are…