zoukankan      html  css  js  c++  java
  • UserDict.IdctMixin制作一个扩展的dict

    http://book.51cto.com/art/201005/198784.htm
    之前一直是继承dict,有些问题,如果以后有需要,可以使用一下这个


    http://snipplr.com/view/6546/creating-dictionarylike-objects-in-python-using-dictmixin/

    The module defines a mixin, DictMixin, defining all dictionary methods for classes that already have a minimum mapping interface. This greatly simplifies writing classes that need to be substitutable for dictionaries (such as the shelve module).


    <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>


  • 相关阅读:
    虚拟机
    10亿数中找出前1000大的数
    deepin云打印实现连接Windows打印机
    如何判断一个数是否在40亿个整数中?
    大三下学期第三周总结
    使用Jenkins构建、部署spring boot项目
    大三下学期第二周总结
    谷歌zxing 二维码生成工具
    Jbarcode 条形码生成工具
    kaptcha验证码实现,配合spring boot使用
  • 原文地址:https://www.cnblogs.com/lexus/p/1801858.html
Copyright © 2011-2022 走看看