Programming tips for everyone

# What Is the Maximum Recursion Depth in Python The maximum recursion depth in Python is 1000.

You can verify this by calling `sys.getrecursionlimit()` function:

```import sys

print(sys.getrecursionlimit()) # Prints 1000```

You can change the limit by calling `sys.setrecursionlimit()` method.

For example:

```import sys

print(sys.setrecursionlimit(2000))```

Consider this a dangerous action!

If possible, instead of tweaking the recursion limit, try to implement your algorithm iteratively to avoid deep recursion.

## Python Maximum Recursion Depth Exceded in Comparison

Whenever you exceed the recursion depth of 1000, you get an error in Python.

For example, if we try to compute a too large Fibonacci number, we get the recursion depth error.

```# A function for computing Fibonacci numbers
def fibonacci(n):
if n <= 1:
return n
else:
return(fibonacci(n-1) + fibonacci(n-2))

# Let's call the 1000th Fibonacci number:
print(fibonacci(1000))```

Output:

```  File "example.py", line 2, in fibonacci
if n <= 1:
RecursionError: maximum recursion depth exceeded in comparison```

This error says it all—maximum recursion depth exceeded in comparison. This tells you that Python’s recursion depth limit of 1000 is reached.

But why is there such a limit? More importantly, how can you overcome it?

## Why Is There a Recursion Depth Limit in Python

A recursive function could call itself indefinitely. In other words, you could end up with an endless loop.

Also, a stack overflow error can occur even if the recursion is not infinite. This can happen due to too big of a stack frame.

In Python, the recursion depth limit takes these risks out of the equation.

Python uses a maximum recursion depth of 1000 to ensure no stack overflow errors and infinite recursions are possible.

This recursion limit is somewhat conservative, but it is reasonable as stack frames can become big in Python.

### What Is a Stack Overflow Error in Python

Stack overflow error is usually caused by too deep (or infinite) recursion.

This means a function calls itself so many times that the space needed to store the information related to each call is more than what fits on the stack.

## How to Change the Recursion Depth Limit in Python—Danger Zone!

You can change the maximum recursion depth in Python. But consider it a dangerous action.

To do this, call the `sys.setrecursionlimit()` function.

For example, let’s set the maximum recursion depth to `2000`:

```import sys

print(sys.setrecursionlimit(2000))```

## Temporarily Change the Recursion Depth Limit in Python

Do you often need to tweak the recursion depth limit in your project?

If you do, consider using a context manager. This can improve the quality of your code.

For example, let’s implement a context manager that temporarily switches the recursion limit:

```import sys

class recursion_depth:
def __init__(self, limit):
self.limit = limit
self.default_limit = sys.getrecursionlimit()

def __enter__(self):
sys.setrecursionlimit(self.limit)

def __exit__(self, type, value, traceback):
sys.setrecursionlimit(self.default_limit)```

Now you can temporarily change the recursion depth to perform a recursive task.

For instance:

```with recursion_depth(2000):
print(fibonacci(1000, 0))```

When this operation completes, the context manager automatically switches the recursion depth limit back to the original value.

Learn more about the `with` statement and context managers in Python here.

## Conclusion

The recursion depth limit in Python is by default `1000`. You can change it using `sys.setrecursionlimit()` function.

Thanks for reading. I hope you enjoy it.

Happy coding!