introduction
Python is known for its simplicity and readability. However, even in Python, you may encounter errors that don’t make sense at first glance. One of those errors is RecursionError: maximum recursion depth exceeded
.
This byte is intended to help you understand what this error is, why it occurs, and how it can be fixed. A basic understanding of Python programming, especially functions, is recommended.
Recursion in Python
Recursion is a fundamental concept in computer science where a function calls itself within its definition. This is a powerful concept that can simplify your code and make it cleaner and more readable depending on the right problem. However, it can also lead to nasty errors if not handled carefully.
Let’s take a look at a simple recursive function in Python.
def factorial(n):
"""Calculate the factorial of a number using recursion"""
if n == 1:
return 1
else:
return n * factorial(n-1)
print(factorial(5))
When you run this code you will see the following 120
, which is a factorial of 5.function factorial
It calls itself with different arguments each time until it reaches the base case (n == 1). Once we reach the base case (n == 1), we start returning results on the call stack.
recursion error
So what happens when a recursive function doesn’t have a good base case, or never reaches a base case? Let’s modify the function above to see the following:
def endless_recursion(n):
"""A recursive function without a proper base case"""
return n * endless_recursion(n-1)
print(endless_recursion(5))
# RecursionError: maximum recursion depth exceeded
When you run this code, RecursionError: maximum recursion depth exceeded
. But why does this happen?
Note: Python has limits on recursion depth to prevent stack overflows. The maximum depth is platform dependent, but is typically around 1000.Beyond this limit, Python RecursionError
.
Cause of RecursionError
of RecursionError: maximum recursion depth exceeded
Python’s safety mechanism. This prevents the program from going into an infinite loop and exhausting all stack space. This error typically occurs when:
- The base case of the recursive function is not defined correctly, or
- Recursive functions do not reach the base case within the maximum recursion depth.
inside endless_recursion
Since the above function has no base case, the function will call itself infinitely, eventually exceeding the maximum depth of recursion.
Fixed RecursionError
When you get RecursionError
, you probably understood that your code is going too deep into recursion. So how do we fix this?
First and foremost, you need to review your code and understand why infinite recursion occurs. Often the problem lies in the base case of recursive functions. Make sure your function has a condition to stop recursion.
Returning to the previous example, RecursionError
:
def endless_recursion(n):
"""A recursive function without a proper base case"""
return n * endless_recursion(n-1)
endless_recursion(5)
To fix this, we need to add a base case that stops recursion when: n
If 0 or less:
def endless_recursion(n):
if n <= 0:
return n
return n * endless_recursion(n-1)
endless_recursion(5)
In some cases, the maximum depth of recursion may be exceeded even though there is a base case. This can occur when processing large amounts of input. In such cases, you can increase the recursion limit using: sys.setrecursionlimit()
.
import sys
sys.setrecursionlimit(3000)
def recursive_function(n):
if n <= 0:
return n
return recursive_function(n-1)
recursive_function(2500)
caveat: Be careful when changing recursion limits. Setting the value too high can cause a stack overflow and cause the program to crash. Always consider the balance between the need for deeper recursion and available system resources.
Python maximum recursion depth
Python’s sys
Modules allow you to access a default maximum recursion depth. To check your current settings, getrecursionlimit()
function. Here’s how to check:
import sys
print(sys.getrecursionlimit())
Typically this will output 1000
However, it may vary depending on the platform.
Changing the maximum recursion depth
I touched on this briefly earlier, but it’s worth going into a little more detail. Although generally not recommended, you can change the maximum recursion depth using the following command: setrecursionlimit()
function from sys
module.
import sys
sys.setrecursionlimit(2000)
This sets the recursion limit to 2000 calls and allows deeper recursion.
Increasing the recursion depth allows recursive functions to perform more calls, which naturally helps algorithms that require deep recursion. However, this comes at the cost of increased memory usage and potential system instability.
Reducing the depth of recursion makes your program more conservative in its resource usage, but it can also make your program more susceptible to problems such as: RecursionError
Even if recursion is logically correct.
Using recursion depth in debugging
One way to debug this kind of problem is to print the current depth of each recursive call. This can help you see if your function is approaching an upper bound or if the recursion isn’t progressing towards the base case as expected.
def factorial(n, depth=1):
print(f"Current recursion depth: depth")
if n == 1:
return 1
else:
return n * factorial(n-1, depth + 1)
print(factorial(5))
In this example, depth
The argument is used to keep track of the current recursion depth. This kind of debug output is very helpful when trying to understand the cause. RecursionError
is happening.
When used together with this, getrecursionlimit()
When profiling your code, it helps you track exactly how close you are to the limits.
conclusion
In this Byte, RecursionError: maximum recursion depth exceeded
In Python. We have discussed how to fix this error and shared tips to avoid this error in the future. We also covered the concepts of the Python stack and recursion depth.