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

Aside: Argument Packing and Unpacking

Argument (un)packing is a peculiar part of Python’s syntax, and unfortunately is not documented particularly well. If you’ve ever seen a function definition like:

def foo(bar, baz, *args, **kwargs):
    ...

or a function call like:

do_something(*packed_args)

these are examples of argument packing and unpacking. Continue reading