- Get and set the recursion limit in Python (sys.getrecursionlimit, setrecursionlimit)
- Get the current value of the recursion limit: sys.getrecursionlimit()
- Set the current value of the recursion limit: sys.setrecursionlimit()
- Change the maximum size of the call stack: resource.setrlimit()
- Related Categories
- Related Articles
- What Is the Maximum Recursion Depth in Python
- Python Maximum Recursion Depth Exceded in Comparison
- Why Is There a Recursion Depth Limit in Python
- What Is a Stack Overflow Error in Python
- How to Change the Recursion Depth Limit in Python—Danger Zone!
- Temporarily Change the Recursion Depth Limit in Python
- Conclusion
Get and set the recursion limit in Python (sys.getrecursionlimit, setrecursionlimit)
Python has the recursion limit, the maximum depth of the Python interpreter stack. If you require deep recursion, you need to set the limit higher with functions in the sys module of the standard library.
The number of recursions is also limited by the stack size. You can change the maximum stack size with the resource module in some environments. It worked on Ubuntu but not on Windows or Mac in my environment.
This article describes the following contents.
- Get the current value of the recursion limit: sys.getrecursionlimit()
- Set the current value of the recursion limit: sys.setrecursionlimit()
- Change the maximum size of the call stack: resource.setrlimit()
The following sample code was run on Ubuntu.
Get the current value of the recursion limit: sys.getrecursionlimit()
You can get the current value of the recursion limit with sys.getrecursionlimit() .
import sys import resource print(sys.getrecursionlimit()) # 1000
In the example, it is 1000 , but it may be different in some environments. The resource module imported here is used later. Note that the resource module is not available on Windows.
Define the following simple recursive function. If a positive integer n is specified, the number of recursions is n .
def recu_test(n): if n == 1: print('Finish') return recu_test(n - 1)
An error ( RecursionError ) is raised if you specify n greater than the recursion limit.
recu_test(950) # Finish # recu_test(1500) # RecursionError: maximum recursion depth exceeded in comparison
Note that the value of sys.getrecursionlimit() is not strictly the maximum number of recursions, but the maximum depth of the Python interpreter stack, so an error is raised even if the number of recursions is slightly less than this value.
# recu_test(995) # RecursionError: maximum recursion depth exceeded while calling a Python object
Set the current value of the recursion limit: sys.setrecursionlimit()
You can set the current value of the recursion limit with sys.setrecursionlimit() .
Larger values allow for deeper recursion.
sys.setrecursionlimit(2000) print(sys.getrecursionlimit()) # 2000 recu_test(1500) # Finish
An error is raised if the specified value is too small or too large.
The highest possible limit is platform-dependent. A user may need to set the limit higher when they have a program that requires deep recursion and a platform that supports a higher limit. This should be done with care, because a too-high limit can lead to a crash.
If the new limit is too low at the current recursion depth, a RecursionError exception is raised. sys.setrecursionlimit() — System-specific parameters and functions — Python 3.10.4 documentation
sys.setrecursionlimit(4) print(sys.getrecursionlimit()) # 4 # sys.setrecursionlimit(3) # RecursionError: cannot set the recursion limit to 3 at the recursion depth 1: the limit is too low sys.setrecursionlimit(10 ** 9) print(sys.getrecursionlimit()) # 1000000000 # sys.setrecursionlimit(10 ** 10) # OverflowError: signed integer is greater than maximum
The recursion limit is also limited by the stack size, as explained next.
Change the maximum size of the call stack: resource.setrlimit()
Even if a large value is set with sys.setrecursionlimit() , you cannot execute a large number of recursions. A segmentation fault occurs as follows.
sys.setrecursionlimit(10 ** 9) print(sys.getrecursionlimit()) # 1000000000
recu_test(10 ** 4) # Finish # recu_test(10 ** 5) # Segmentation fault
In Python, you can change the maximum size of the call stack with the resource module in the standard library. Note that the resource module is Unix-specific and cannot be used on Windows.
You can get the limit of the resource as (soft limit, hard limit) with resource.getrlimit() . Specify resource.RLIMIT_STACK , which represents the maximum size of the call stack of the current process, as a resource.
print(resource.getrlimit(resource.RLIMIT_STACK)) # (8388608, -1)
In this example, the soft limit is 8388608 (8388608 B = 8192 KB = 8 MB), and the hard limit is -1 (unlimited).
You can change the limit of the resource with resource.setrlimit() .
Setting the soft limit to -1 allows deep recursion that could not be executed before.
resource.setrlimit(resource.RLIMIT_STACK, (-1, -1)) print(resource.getrlimit(resource.RLIMIT_STACK)) # (-1, -1) recu_test(10 ** 5) # Finish
Here the soft limit is set to -1 for the sake of experimentation, but in practice, it would be safer to limit it to an appropriate value.
Note that an error, ValueError: not allowed to raise maximum limit , is raised on Mac when the soft limit is set to -1 . Running the script with sudo did not work. Maybe it is limited by the system.
A process with the effective UID of super-user can request any valid limit value, including unlimited, but ValueError will still be raised if the requested limit exceeds the system imposed limit. resource.setrlimit() — Resource usage information — Python 3.10.4 documentation
Related Categories
Related Articles
- Missing values in pandas (nan, None, pd.NA)
- Regular expressions with the re module in Python
- Move a file/directory in Python (shutil.move)
- nan (not a number) in Python
- pandas: Handle strings (replace, strip, case conversion, etc.)
- Complex numbers in Python
- Find the number of days and weeks of a month in Python
- Remove/extract duplicate elements from list in Python
- Convert list and tuple to each other in Python
- numpy.where(): Manipulate elements depending on conditions
- Keywords and reserved words in Python
- Check if key/value exists in dictionary in Python
- The pass statement in Python
- Find GCD and LCM in Python (math.gcd(), lcm())
- Save frames from video files as still images with OpenCV in Python
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.
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 nFile "example.py", line 2, in fibonacci if nThis 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?
Let’s answer these questions next.
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.
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.