I think I&#39;d go along with Laura and Vernon as long as it&#39;s made clear that one should specify the exception, as Vernon does in his sample. Bare excepts should NOT be encouraged, particularly since I&#39;ve seen some students find them way too seductive - &quot;Look, I can handle EVERY error!&quot; ;) <div>
<br></div><div>OTOH the .get() method has always struck me as imposing a bit of a cognitive load that I could live without.<div><br></div><div>Vern<br><br><div class="gmail_quote">On Wed, Dec 14, 2011 at 3:42 PM, Vernon Cole <span dir="ltr">&lt;<a href="mailto:vernondcole@gmail.com">vernondcole@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Sorry, Kirby, I&#39;m afraid I disagree.<br>
       try:<br>
           res_dict[ext] += 1<br>
       except KeyError:<br>
           res_dict[ext] = 1<br>
is clear in intent and operation. It is a self-initializing occurrence counter.<br>
<br>
On the other hand,<br>
      res_dict[ext] = res_dict.get(ext, 0) + 1<br>
is obscure.  Unless I go dig up the documentation of what the .get()<br>
operation does on a dictionary, I have no idea what the intention of<br>
the code is -- and I am (humbly) a very experienced programmer and<br>
Python programmer.  Also I doubt whether the one-line version is<br>
faster.  Without looking at the code, I would bet that .get() is<br>
implemented using a try / except structure. There is nothing wrong<br>
with that.<br>
<br>
Remember the Zen of Python:<br>
&gt;&gt;&gt;import this<br>
...<br>
Readability counts.<br>
...<br>
<span class="HOEnZb"><font color="#888888">--<br>
Vernon<br>
</font></span><div class="HOEnZb"><div class="h5">_______________________________________________<br>
Edu-sig mailing list<br>
<a href="mailto:Edu-sig@python.org">Edu-sig@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/edu-sig" target="_blank">http://mail.python.org/mailman/listinfo/edu-sig</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>Vern Ceder<br><a href="mailto:vceder@gmail.com" target="_blank">vceder@gmail.com</a>, <a href="mailto:vceder@dogsinmotion.com" target="_blank">vceder@dogsinmotion.com</a><br>
The Quick Python Book, 2nd Ed - <a href="http://bit.ly/bRsWDW" target="_blank">http://bit.ly/bRsWDW</a><br><br><br>
</div></div>