http://book.51cto.com/art/201005/198784.htm
之前一直是继承dict,有些问题,如果以后有需要,可以使用一下这个
http://snipplr.com/view/6546/creating-dictionarylike-objects-in-python-using-dictmixin/
之前一直是继承dict,有些问题,如果以后有需要,可以使用一下这个
http://snipplr.com/view/6546/creating-dictionarylike-objects-in-python-using-dictmixin/
<ol><li class="li1"><div class="de1"><span class="st0">"""How to create a custom mappable container (dictionary-like) type in Python."""</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"><span class="kw1">from</span> <span class="kw3">UserDict</span> <span class="kw1">import</span> DictMixin</div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"><span class="kw1">class</span> MyDict<span class="br0">(</span>DictMixin<span class="br0">)</span>:</div></li><li class="li1"><div class="de1"> <span class="co1"># MyDict only needs to implement getitem, setitem, delitem and keys (at a </span></div></li><li class="li1"><div class="de1"> <span class="co1"># minimum) and UserDict will provide the rest of the standard dictionary</span></div></li><li class="li1"><div class="de1"> <span class="co1"># methods based on these four.</span></div></li><li class="li1"><div class="de1"> <span class="co1">#</span></div></li><li class="li1"><div class="de1"> <span class="co1"># getitem and delitem should raise KeyError if no item exists for the given</span></div></li><li class="li1"><div class="de1"> <span class="co1"># key. getitem, setitem and delitem should raise TypeError if the given key</span></div></li><li class="li1"><div class="de1"> <span class="co1"># is of the wrong type.</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="kw1">def</span> <span class="kw4">__getitem__</span><span class="br0">(</span><span class="kw2">self</span>, key<span class="br0">)</span>:</div></li><li class="li1"><div class="de1"> ....</div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="kw1">def</span> <span class="kw4">__setitem__</span><span class="br0">(</span><span class="kw2">self</span>, key, item<span class="br0">)</span>:</div></li><li class="li1"><div class="de1"> ....</div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="kw1">def</span> <span class="kw4">__delitem__</span><span class="br0">(</span><span class="kw2">self</span>, key<span class="br0">)</span>:</div></li><li class="li1"><div class="de1"> ....</div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="kw1">def</span> keys<span class="br0">(</span><span class="kw2">self</span><span class="br0">)</span>:</div></li><li class="li1"><div class="de1"> ....</div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"><span class="co1"># You can now use your class as if it was a dict, using the standard container </span></div></li><li class="li1"><div class="de1"><span class="co1"># operators and dictionary methods.</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1">d = MyDict<span class="br0">(</span><span class="br0">)</span></div></li><li class="li1"><div class="de1">d<span class="br0">[</span>key<span class="br0">]</span> = value</div></li><li class="li1"><div class="de1">d.<span class="me1">get</span><span class="br0">(</span>key<span class="br0">)</span></div></li><li class="li1"><div class="de1">d.<span class="me1">clear</span><span class="br0">(</span><span class="br0">)</span></div></li><li class="li1"><div class="de1">etc.</div></li></ol>
![](http://img.zemanta.com/pixy.gif?x-id=98ad1d9a-0c8d-82f3-9bfa-8e21426f2026)