How does one create an generalise AttrDict Mixin for a python "dict-style" class?
<p>Basically I want to be able to take <code>class</code> that <em>quacks</em> like a <code>dict</code> (eg my example <code>DICT</code> below) and add a <code>MixIn</code> that allows me to access the <code>DICT</code>'s values as attributes... eg </p>

<pre><code>print purchase.price # instead of purchase["price"]
</code></pre>

<p>Background: I have a various different database tables (not just the <code>DICT</code> example) that look (&amp; <em>quack</em>) like a <code>dict</code> (eg. <code>bsddb</code>) and I want define (then use) a standard <code>AttrDictMixIn</code>.  (And so avoid using a boilerplate cut/paste of code)</p>

<pre><code># First try using a std dict
class AttrDict(dict):
    def __init__(self, *args, **kwargs):
        self.__dict__ = self
        super(AttrDict, self).__init__(*args, **kwargs)

test1=AttrDict()
test1.x="111"
print 1,test1

# Next derive a crude UPPER dict
class DICT(dict): # EXAMPLE ONLY
  def __getitem__(self,key):
    return super(DICT,self).__getitem__(key.upper())

  def __setitem__(self,key,value):
    return super(DICT,self).__setitem__(key.upper(),value)

test2=DICT()
test2["x"]="222"
print 2,test2

# Next define a AttrDict MixIn
class AttrDictMixIn(object):  # This is what I want to work...
    def __init__(self, *args, **kwargs):
        self.__dict__ = self
        return super(AttrDict, self).__init__(*args, **kwargs)

# Apply the MixIn to DICT 
class AttrDICT(DICT,AttrDictMixIn): pass

test3=AttrDICT()
test3.x="333.xxx"
test3["y"]="333.yyy"
print 3,test3,"X is missing"
print 4,"test3.x:",test3.x,"OK"
print 5,"test3['y']:",test3["y"],"OK"
print 6,"test3.y:",test3.y,"DUD"
print 7,"test3['x']:",test3["x"],"DUD"
</code></pre>

<p>Output:</p>

<pre><code>1 {'x': '111'}
2 {'X': '222'}
3 {'Y': '333.yyy'} X is missing
4 test3.x: 333.xxx OK
5 test3['y']: 333.yyy OK
6 test3.y:
Traceback (most recent call last):
  File "x.py", line 39, in &lt;module&gt;
    print 6,"test3.y:",test3.y,"DUD"
AttributeError: 'AttrDICT' object has no attribute 'y'
</code></pre>

<p>I suspect I am doing something trivial wrong... Hints welcome.
(And pointers to some reference examples of similar python MixIns may also help)</p>

<p>edit: an explanation of why the line <code>self.__dict__ = self</code> breaks multiple inheritance would be appreciated.</p>

<p>Implement it like so:</p>

<pre><code>class AttrDictMixin(object):
    def __getattr__(self, name):
        return self[name]
    def __setattr__(self, name, value):
        self[name] = value
    def __delattr__(self, name):
        del self[name]
</code></pre>

<p>Upon attribute access, setting, or deleting - <code>__getattr__</code>, <code>__setattr__</code> and <code>__delattr__</code> are called accordingly.</p>

<p>Keep in mind mixins should come before the base class in the inheritance:</p>

<pre><code>class AttrDict(AttrDictMixin, DICT): pass
</code></pre>

python class dictionary attributes multiple-inheritance