转载请注明转自: 团子的小窝 , 本文固定链接: defaultdict 和 dict.__missing__
http://blog.csdn.net/topfire7/article/details/19168799
原作者: Jason Kirtland
日期: January 13th, 2009
许可证: Creative Commons Attribution-Share Alike 3.0
原文链接(PPT): http://discorporate.us/jek/talks/defaultdict/
翻译作者:kodango <dangoakachan@foxmail.com>
翻译时间:March 17th, 2012
译文链接:http://kodango.com/defaultdict-in-python
今天看到一篇讲defaultdict的PPT,同时第一次见到__missing__()
这个方法,好奇之下,仔细阅读了这篇PPT。看完之后随手做笔记,分享给有需要的人。准确地说,这篇文章不是纯粹的翻译,因为原文本身只是一份PPT。文章的大多数文字内容,都是本人的阅读心得。
默认值可以很方便
众所周知,在Python中如果访问字典中不存在的键,会引发KeyError异常(JavaScript中如果对象中不存在某个属性,则返回undefined)。但是有时候,字典中的每个键都存在默认值是非常方便的。例如下面的例子:
<span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">strings </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'kitten'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'weasel'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'kitten'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
counts </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">{}</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">for</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> kw </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">in</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> strings</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
counts</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">kw</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">+=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">1</span>
该例子统计strings中某个单词出现的次数,并在counts字典中作记录。单词每出现一次,在counts相对应的键所存的值数字加1。但是事实上,运行这段代码会抛出KeyError异常,出现的时机是每个单词第一次统计的时候,因为Python的dict中不存在默认值的说法,可以在Python命令行中验证:
<span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> counts </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">()</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> counts
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">{}</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> counts</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">+=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">Traceback</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">most recent call last</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">File</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">"<stdin>"</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> line </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">in</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"><</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">module</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">KeyError</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span>
使用判断语句检查
既然如此,首先可能想到的方法是在单词第一次统计的时候,在counts中相应的键存下默认值1。这需要在处理的时候添加一个判断语句:
<span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">strings </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'kitten'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'weasel'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'kitten'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
counts </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">{}</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">for</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> kw </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">in</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> strings</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">if</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> kw </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">not</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">in</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> counts</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
counts</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">kw</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">else</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
counts</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">kw</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">+=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="com" style="font-family:inherit;color:#999988;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:italic; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"># counts:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="com" style="font-family:inherit;color:#999988;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:italic; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"># {'puppy': 5, 'weasel': 1, 'kitten': 2}</span>
使用dict.setdefault()
方法
也可以通过dict.setdefault()
方法来设置默认值:
<span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">strings </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'kitten'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'weasel'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'kitten'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
counts </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">{}</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">for</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> kw </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">in</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> strings</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
counts</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">setdefault</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">kw</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">0</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
counts</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">kw</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">+=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="com" style="font-family:inherit;color:#999988;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:italic; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"># 原PPT中这里有一个笔误</span>
dict.setdefault()
方法接收两个参数,第一个参数是健的名称,第二个参数是默认值。假如字典中不存在给定的键,则返回参数中提供的默认值;反之,则返回字典中保存的值。利用dict.setdefault()
方法的返回值可以重写for循环中的代码,使其更加简洁:
<span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">strings </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'kitten'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'weasel'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'kitten'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
counts </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">{}</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">for</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> kw </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">in</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> strings</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
counts</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">kw</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> counts</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">setdefault</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">kw</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">0</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">+</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">1</span>
使用collections.defaultdict
类
以上的方法虽然在一定程度上解决了dict中不存在默认值的问题,但是这时候我们会想,有没有一种字典它本身提供了默认值的功能呢?答案是肯定的,那就是collections.defaultdict
。
defaultdict类就好像是一个dict,但是它是使用一个类型来初始化的:
<span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">from</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> collections </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">import</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> defaultdict
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dd </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> defaultdict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">list</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dd
defaultdict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(<</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">type </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'list'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">{})</span>
defaultdict类的初始化函数接受一个类型作为参数,当所访问的键不存在的时候,可以实例化一个值作为默认值:
<span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dd</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'foo'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dd
defaultdict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(<</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">type </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'list'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">{</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'foo'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[]})</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dd</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'bar'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">].</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">append</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'quux'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dd
defaultdict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(<</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">type </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'list'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">{</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'foo'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[],</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'bar'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'quux'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]})</span>
需要注意的是,这种形式的默认值只有在通过dict[key]
或者dict.__getitem__(key)
访问的时候才有效,这其中的原因在下文会介绍。
<span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">from</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> collections </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">import</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> defaultdict
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dd </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> defaultdict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">list</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'something'</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">in</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dd
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">False</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dd</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">pop</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'something'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">Traceback</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">most recent call last</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">File</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">"<stdin>"</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> line </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">in</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"><</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">module</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">KeyError</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'pop(): dictionary is empty'</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dd</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">get</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'something'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dd</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'something'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[]</span>
defaultdict类除了接受类型名称作为初始化函数的参数之外,还可以使用任何不带参数的可调用函数,到时该函数的返回结果作为默认值,这样使得默认值的取值更加灵活。下面用一个例子来说明,如何用自定义的不带参数的函数zero()作为defaultdict类的初始化函数的参数:
<span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">from</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> collections </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">import</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> defaultdict
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> zero</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">():</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">...</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">0</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">...</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dd </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> defaultdict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">zero</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dd
defaultdict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(<</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">function zero at </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">0xb7ed2684</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">{})</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dd</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'foo'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">0</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dd
defaultdict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(<</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">function zero at </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">0xb7ed2684</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">{</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'foo'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">0</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">})</span>
利用collections.defaultdict
来解决最初的单词统计问题,代码如下:
<span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">from</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> collections </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">import</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> defaultdict
strings </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'kitten'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'weasel'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'kitten'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'puppy'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
counts </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> defaultdict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">lambda</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">0</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="com" style="font-family:inherit;color:#999988;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:italic; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"># 使用lambda来定义简单的函数</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">for</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> s </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">in</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> strings</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
counts</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">s</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">+=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">1</span>
defaultdict类是如何实现的
通过上面的内容,想必大家已经了解了defaultdict类的用法,那么在defaultdict类中又是如何来实现默认值的功能呢?这其中的关键是使用了看__missing__()
这个方法:
<span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">from</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> collections </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">import</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> defaultdict
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">print</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> defaultdict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">__missing__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">__doc__
__missing__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="com" style="font-family:inherit;color:#999988;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:italic; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"># Called by __getitem__ for missing key; pseudo-code:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">if</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">default_factory </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">is</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">None</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">raise</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">KeyError</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> value </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">default_factory</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">()</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> value</span>
通过查看__missing__()
方法的docstring,可以看出当使用__getitem__()
方法访问一个不存在的键时(dict[key]这种形式实际上是__getitem__()
方法的简化形式),会调用__missing__()
方法获取默认值,并将该键添加到字典中去。
关于__missing__()
方法的具体介绍可以参考Python官方文档中的"Mapping Types — dict"一节。
文档中介绍,从2.5版本开始,如果派生自dict的子类定义了__missing__()
方法,当访问不存在的键时,dict[key]会调用__missing__()
方法取得默认值。
从中可以看出,虽然dict支持__missing__()
方法,但是在dict本身是不存在这个方法的,而是需要在派生的子类中自行实现这个方法。可以简单的验证这一点:
<span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">print</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">__missing__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">__doc__
</span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">Traceback</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">most recent call last</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">File</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">"<stdin>"</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> line </span><span class="lit" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">in</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"><</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">module</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">AttributeError</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> type object </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'dict'</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> has no attribute </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'__missing__'</span>
同时,我们可以进一步的做实验,定义一个子类Missing并实现__missing__()
方法:
<span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">class</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">Missing</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">dict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">...</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> __missing__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">...</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'missing'</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">...</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> d </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">Missing</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">()</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> d
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">{}</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> d</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'foo'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'missing'</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> d
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">{}</span>
返回结果反映了__missing__()
方法确实发挥了作用。在此基础上,我们稍许修改__missing__()
方法,使得该子类同defautldict类一样为不存在的键设置一个默认值:
<span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">class</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">Defaulting</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">dict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">...</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> __missing__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">...</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'default'</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">...</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'default'</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">...</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> d </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">Defaulting</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">()</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> d
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">{}</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> d</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'foo'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'default'</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">>>></span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> d
</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">{</span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'foo'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">'default'</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">}</span>
在旧版本的Python中实现类defaultdict的功能
defaultdict类是从2.5版本之后才添加的,在一些旧版本中并不支持它,因此为旧版本实现一个兼容的defaultdict类是必要的。这其实很简单,虽然性能可能未必如2.5版本中自带的defautldict类好,但在功能上是一样的。
首先,__getitem__()
方法需要在访问键失败时,调用__missing__()
方法:
<span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">class</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> defaultdict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">dict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> __getitem__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">try</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">__getitem__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">except</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">KeyError</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">__missing__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span>
其次,需要实现__missing__()
方法用来设置默认值:
<span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">class</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> defaultdict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">dict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> __getitem__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">try</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">__getitem__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">except</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">KeyError</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">__missing__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> __missing__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> value </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">default_factory</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">()</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> value</span>
然后,defaultdict类的初始化函数__init__()
需要接受类型或者可调用函数参数:
<span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">class</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> defaultdict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">dict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> __init__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> default_factory</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">None</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">*</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">a</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">**</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">kw</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
dict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">__init__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">*</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">a</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">**</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">kw</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">default_factory </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> default_factory
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> __getitem__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">try</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">__getitem__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">except</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">KeyError</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">__missing__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> __missing__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> value </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">default_factory</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">()</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> value</span>
最后,综合以上内容,通过以下方式完成兼容新旧Python版本的代码:
<span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">try</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">from</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> collections </span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">import</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> defaultdict
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">except</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">ImportError</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">class</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> defaultdict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">dict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> __init__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> default_factory</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">None</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">*</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">a</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">**</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">kw</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
dict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">__init__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">*</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">a</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">**</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">kw</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">default_factory </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> default_factory
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> __getitem__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">try</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> dict</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">__getitem__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">except</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#445588;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">KeyError</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">__missing__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> __missing__</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">[</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">key</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> value </span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> self</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">default_factory</span><span class="pun" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">()</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="kwd" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:bold; line-height:inherit; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#333333;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;"> value</span>