<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Object Mentor Blog: Tag pitfall</title>
    <link>http://blog.objectmentor.com/articles/tag/pitfall</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Python's Mutable Default Problem</title>
      <description>&lt;p&gt;Today I was perusing &lt;a href="http://www.siafoo.net/article/52"&gt;a very fine python article&lt;/a&gt; all professional python programmers should read. I had to grin when I saw one of my favorite python quirks/flaws.&lt;/p&gt;


Python only evaluates default parameters when it defines a function, leading to surprising side effects &lt;i&gt;(example from saifoo)&lt;/i&gt;:
&lt;pre&gt;
def function(item, stuff = []):
    stuff.append(item)
    print stuff

function(1)
# prints '[1]'

function(2)
# prints '[1,2]' !!!
&lt;/pre&gt;

	&lt;p&gt;The grin on my face was because I was bit by this one again last Tuesday.  You just don&amp;#8217;t want to make a mutable object a default parameter. The solution I used was close to the solution from saifoo:&lt;/p&gt;


Saifoo.net:
&lt;pre&gt;
def function(item, stuff=None):
   if stuff is None:
      stuff = []
   stuff.append(item)
   print stuff
&lt;/pre&gt;
This, of course, gives the correct behavior because it creates a new empty list each time it is called without a second parameter.

	&lt;p&gt;My solution actually combines some advice contained earlier in the article with the solution given for this problem:&lt;/p&gt;


Mine
&lt;pre&gt;
def function(item, stuff=None):
   stuff = stuff or []
   stuff.append(item)
   print stuff
&lt;/pre&gt;

	&lt;p&gt;It&amp;#8217;s not necessarily prettier, and I struggle with whether it is more obvious (to a Python programmer) or not.  I fear it may be &lt;i&gt;clever&lt;/i&gt; (a word I only use in the pejorative sense), but it is also clean-looking to me.&lt;/p&gt;


	&lt;p&gt;The point remains, though, that the mutable default parameter quirk is an ugly corner worth avoiding, and I suspect that any code that purposefully exploits that behavior to populate a structure that persists across calls will run a very real risk of being misunderstood.&lt;/p&gt;


	&lt;p&gt;We all know what happens when the next programmer to touch a program misunderstands it.&lt;/p&gt;</description>
      <pubDate>Thu, 22 May 2008 22:16:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:36360a07-a7f0-4ea1-a246-52ce62b71996</guid>
      <author>Tim Ottinger</author>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem</link>
      <category>Tim's Tepid Torrent</category>
      <category>python</category>
      <category>mutable</category>
      <category>default</category>
      <category>pitfall</category>
    </item>
  </channel>
</rss>
