Finding Which Superclass Provides a Method

Here's a quick hack to help when you're lost in a too-large Python inheritance tree. What class provides the most specific implementation of a method or attribute?

def find(cls, name):
    for sup in cls.mro():
        if name in sup.__dict__:
            return sup.__name__
    return None

def find_all_vars(cls):
    for …
more ...

Thread Locals in Python: Mostly easy

Thread locals are an interesting idea: a way to give each thread its own storage, useful for global state that you don't want to share between threads.

The obvious caveat is that threadlocals are still effectively global (for the current thread), and like all global state, should be treated with …

more ...