Python style: use imported class as namespace for related custom data?
Question: I was wondering if it's ok to use an imported, 3rd-party class as a namespace
for you related custom variables? Say, the following code:

    
    
    import pycurl
    
    curlm = pycurl.CurlMulti()
    curlm.pool = [pycurl.Curl() for i in range(POOL_SIZE)]
    curlm.running = set()
    

In this example, pycurl.CurlMulti is some 3rd-party class and _pool_ and
_running_ are my variables (not provided by CurlMulti), but intended to be
used together with curlm.


Answer: Don't do it. What you want is a
[WeakKeyDictionary](http://docs.python.org/2/library/weakref.html#weakref.WeakKeyDictionary).
You can associate arbitrary values with arbitrary objects without worrying
about different modules overwriting each other's variables or the dict keeping
objects alive too long.

Suppose you write a module `foo.py` that sets `thing.whatever = 5`. Two Python
versions later, `thing` offers a `whatever` method. Everything breaks, and you
didn't even touch your code.

Suppose you write a module `foo.py` that sets `thing.whatever = 5`. Two months
later, you're working on a different project, and you write a module `bar.py`
that sets `thing.whatever = 4` on the same `thing`. Horrible breakage occurs
due to unrelated decisions you made months ago.

