You’ll be able to
- Write recursive functions with clear base cases
- Recognize when recursion is clearer than iteration
- Avoid stack overflow with tail-call awareness
- Convert simple recursion to iteration when needed
Loading lesson…
Why this matters
Python caps recursion at ~1000 frames (sys.getrecursionlimit), so tree traversals in AST tooling, JSON walkers, and directory scanners often need iterative rewrites. functools.lru_cache on recursive functions is the standard interview trick for memoization.
Common pitfalls
- Missing or wrong base case — you'll hit
RecursionErrorfast; write the base case first, then the recursive step. - Deep recursion on user input — an attacker can crash your parser; convert to a stack-based loop for JSON/XML.
- Recomputing overlapping subproblems — wrap the function with
@lru_cache(maxsize=None)for instant speedup.