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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

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 Comment Block / Python Multiline Comment

Python Comment Block

Table of Contents Hide Introduction to Python Comment BlockTypes of comments in PythonSingle-line commentsInline commentsMultiline commentsUsing Multiple Hashtags (#)Python docstringsSingle line docstringsMulti-line docstrings Comments are a piece of text in…
View Post