    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Python has setdefault</title>
        <link>https://members.accu.org/index.php/articles/2735</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + CVu Journal Vol 31, #6 - January 2020</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c406/">316</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-406/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+406/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Python has setdefault</h1>
<p><strong>Author:</strong>&nbsp;Bob Schmidt</p>
<p>
<strong>Date:</strong> 06 January 2020 18:00:30 +00:00 or Mon, 06 January 2020 18:00:30 +00:00</p>
<p><strong>Summary:</strong>&nbsp;Silas S. Brown shares a quick tip on Python.</p>
<p><strong>Body:</strong>&nbsp;<p>I learned Python in the days when Python 1.x was still around, incidentally as a result of reviewing <em>Professional Linux Programming</em> by Neil Matthew and Richard Stones, in <em>CVu</em> 13(2), April 2001. Python 2.0 had been released in October 2000 but it took a while for it to be fully integrated into GNU/Linux distributions etc. I did start using Python 2 whenever possible (not least because of its Unicode support) and made 2.0 my code's minimum requirement, but I only recently discovered in 2019 that all these years Iâ€™ve been using a certain Python 1.x idiom that 2.x has a single instruction for.</p>

<p>So, public service announcement (in case anybody else has been labouring under the same incomplete understanding all these years), if you often write code like this:</p>

<pre class="programlisting">
  if not myDict.has_key(k): myDict[k] = []
  myDict[k].append(s)</pre>
  
<p>then you are still in the 1.x days on two counts: use of <code>has_key</code> (nowadays you can simply say <code>if not k in myDict</code>), and non-use of <code>setdefault</code>. In every version of Python from 2.0 onwards, the above is equivalent to:</p>

<pre class="programlisting">
  myDict.setdefault(k,[]).append(s)</pre>
  
<p>and the only reason I can think of not to use this is if the constructor to the <code>[]</code> were so much overhead that you really donâ€™t want to run it unless needed (which isnâ€™t the case for the empty list, but might be the case for some user-defined type). Otherwise, <code>setdefault</code> away.</p>

<p class="bio"><span class="author"><b>Silas S. Brown</b></span> Silas is a partially-sighted Computer Science post-doc in Cambridge who currently works in part-time assistant tuition and part-time for Oracle. He has been an ACCU member since 1994.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
