What is the maximum recursion depth, and how to increase it?
The default Python recursion limit is 1000
, set to prevent the possibility of a stack overflow from infinite recursion. To modify it, use sys.setrecursionlimit(limit)
. However, tread with caution; high limits might possibly result in system crashes.
Take note, Python doesn't enjoy deep recursion due to the absence of tail recursion optimization.
Plate check: What's the current recursion depth?
Before diving headfirst into the pool, check how deep it is. Use sys.getrecursionlimit()
to pull up the current limit:
Iterative Solutions: A Loop...Hole?
Given Python's large stack frames, an iterative solution might just be your safety cushion. This completely bypasses the recursion limit and offers better memory efficiency. Use loops, stacks and queues to roll your own "call stack."
Beyond just sys.setrecursionlimit()
Temporary limit boosts: Context Managers
Need a quick increase in recursion limit for a particular block of code? Context managers to the rescue! This allows raising the limit for a specific block and then restore the old values once done:
System Limits: The bigger picture
The shell command ulimit
displays the current stack size limit, usually about 8MB. Run ulimit -s
to check the current limit and ulimit -s new_size
to set a new limit. Obviously, size does matter here.
Echoing system and Python limits
While pushing limits, keep an eye on the system's RAM. A sky-high setrlimit
might sap up the RAM, causing broader system issues. It is often wiser to adjust both Python's limit and the OS stack size with careful thought.
Recursive function redesign to the rescue
Sometimes, it's the design that matters. Redesigning a recursive function to minimise the recursion depth can be a lifesaver. Flatten your functions, use helper functions or simply wave goodbye to recursion and move to an iterative approach.
Was this article helpful?