Using Nested Functions for Cleaner Recursion

Python is not a particularly friendly language for recursion — a finite stack and significant overhead on function calls often make alternate implementations more desirable. However, there are some problems that are difficult to handle gracefully any other way — tree structures, for example — and other instances where clarity is preferable to performance. They do still have a place, and where we use them we may as well try to make them as clean and elegant as possible. Fortunately, Python makes this rather easy.
Continue reading