Python 2 __missing__ method
<p>I wrote a very simple program to subclass a dictionary. I wanted to try the <code>__missing__</code> method in python.
After some research i found out that in Python 2 it's available in <code>defaultdict</code>. ( In python 3 we use collections.UserDict though..)
The <code>__getitem__</code> is the on responsible for calling the <code>__missing__</code> method if the key isn't found. </p>

<p>When i implement <code>__getitem__</code> in the following program i get a key error, but when i implement without it, i get the desired value. </p>

<pre><code>import collections
class DictSubclass(collections.defaultdict):

    def __init__(self,dic):
        if dic is None:
            self.data = None
        else:
            self.data = dic

    def __setitem__(self,key,value):
        self.data[key] = value

    ########################
    def __getitem__(self,key):
        return self.data[key]
    ########################

    def __missing__(self,key):
        self.data[key] = None

dic = {'a':4,'b':10}
d1 = DictSubclass(dic)
d2 = DictSubclass(None)    
print  d1[2]
</code></pre>

<p>I thought i needed to implement <code>__getitem__</code> since it's responsible for calling <code>__missing__</code>. I understand that the class definition of defaultdict has a <code>__getitem__</code> method. But even so, say i wanted to write my own <code>__getitem__</code>, how would i do it?</p>