<?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: Python's Mutable Default Problem</title>
    <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem</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 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:36360a07-a7f0-4ea1-a246-52ce62b71996</guid>
      <author>tottinger</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>
    <item>
      <title>"Python's Mutable Default Problem" by iphone fix</title>
      <description>&lt;p&gt;hope that the problem finally solved.&lt;/p&gt;</description>
      <pubDate>Tue, 09 Mar 2010 21:30:20 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:e668546b-e0b5-4b21-bc58-7ae31edd3a65</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-7820</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by gucci louis vuitton shoes</title>
      <description>&lt;p&gt;Welcome to Freshstyleshop, the hottest urban clothing site on the net! We offer great products from Gucci sneakers, prada sneakers, LV shoes, True Religion Jeans and many more! Our selection of products are always increasing for the fact that we have new items added weekly to our selection. All products on our site are already marked down 40-60% off retail price. Freshstyleshop also backs all its orders with a 110% satisfaction guarantee, making sure that our customers are left satisfied with the hottest products on the net.&lt;/p&gt;</description>
      <pubDate>Tue, 15 Dec 2009 21:19:39 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:f24a031d-670e-407a-aadf-1a3d4d0575f8</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-5726</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by dvla address</title>
      <description>&lt;p&gt;So far as I know, I&#8217;ve not seen it used on purpose. I&#8217;ve accidentally used it a few times. The tinker in me wants to try using it on purpose, and the professional refuses.&lt;/p&gt;</description>
      <pubDate>Wed, 02 Sep 2009 11:23:57 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:d299203c-ae51-4874-a353-aeaca1111c07</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-4025</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by Tim Ottinger</title>
      <description>&lt;p&gt;Excellent catch.  Should have checked that it was None instead.  thank you for the update!&lt;/p&gt;</description>
      <pubDate>Fri, 19 Jun 2009 16:40:11 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:b77ec5be-3a35-4413-9b19-d4b6792dc9d2</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-3600</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by Olivier</title>
      <description>&lt;p&gt;I know it&amp;#8217;s late, but I came across this page from Google, so in case someone else would look into it&amp;#8230;&lt;/p&gt;


	&lt;p&gt;Your own &amp;#8220;fix&amp;#8221; is incorrect, because an empty list evaluates to False in Python, so that if you try to use your function to append some item to an existing, yet empty, list, it won&amp;#8217;t work.&lt;/p&gt;</description>
      <pubDate>Thu, 18 Jun 2009 10:16:34 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:93bd07d4-70c7-4d94-b700-0e9b98fd51c0</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-3587</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by tim</title>
      <description>&lt;p&gt;@JustRanting:&lt;/p&gt;


	&lt;p&gt;I guess about like this:&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://www.google.com/search?q=python+professional&amp;#38" rel="nofollow"&gt;http://www.google.com/search?q=python+professional&amp;#38&lt;/a&gt;;ie=utf-8&amp;#38;oe=utf-8&amp;#38;aq=t&amp;#38;rls=org.mozilla:en-US:official&amp;#38;client=firefox-a&lt;/p&gt;


	&lt;p&gt;Or this:
&lt;a href="http://www.google.com/trends?q=python%2C+professional&amp;#38" rel="nofollow"&gt;http://www.google.com/trends?q=python%2C+professional&amp;#38&lt;/a&gt;;ctab=0&amp;#38;hl=en&lt;/p&gt;


	&lt;p&gt;Or maybe this:
&lt;a href="http://www.google.com/search?q=professional+python+job&amp;#38" rel="nofollow"&gt;http://www.google.com/search?q=professional+python+job&amp;#38&lt;/a&gt;;ie=utf-8&amp;#38;oe=utf-8&amp;#38;aq=t&amp;#38;rls=org.mozilla:en-US:official&amp;#38;client=firefox-a&lt;/p&gt;


	&lt;p&gt;There is actually a lot more python out there than you think there is.  And probably less than I hope. ;-)&lt;/p&gt;</description>
      <pubDate>Mon, 23 Jun 2008 16:45:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:c4914f21-ca64-440f-aa75-2164d0cdf27a</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-1847</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by JustRanting</title>
      <description>&lt;p&gt;I wonder how Python and professional fit together ;-)&lt;/p&gt;</description>
      <pubDate>Fri, 20 Jun 2008 14:37:03 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:c609d7f7-31cf-40a5-b069-9f7991a30f60</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-1843</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by yachris2112</title>
      <description>&lt;p&gt;Once again, I have to point out that Lisp had this first :-).  I remember (&lt;em&gt;way&lt;/em&gt;) back in the day having this explained to me by none other than David Moon.  For pythonistas, this is like seeing a noob post responded to by Guido.&lt;/p&gt;


	&lt;p&gt;Anyway, Ruby has a similar idiom:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;somevar ||= default_val&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;although the &amp;#8216;or&amp;#8217; syntax would work as well.  I think the &amp;#8217;||&amp;#8217; version is a bit more noticeable.&lt;/p&gt;


	&lt;p&gt;matthew: almost certainly not.&lt;/p&gt;</description>
      <pubDate>Tue, 27 May 2008 07:31:37 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:9a126b88-29aa-4347-aa35-293be9e802a2</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-1784</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by matthew</title>
      <description>&lt;p&gt;I wonder if this was a deliberate decision, to aid with recursion?&lt;/p&gt;</description>
      <pubDate>Fri, 23 May 2008 11:34:29 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:5dcbf6d2-63ea-4d3a-b8fa-fccc65139712</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-1782</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by John Roth</title>
      <description>&lt;p&gt;Two comments. First, the or isn&amp;#8217;t a hack. It&amp;#8217;s a standard Python idiom, although it looks a bit strange to someone who doesn&amp;#8217;t have a lot of Python experience.&lt;/p&gt;


	&lt;p&gt;Second, having a mutable object as a parameter is definitely a gotcha for most Python programmers. The interesting thing is that it could be used as a way of getting a persistent variable into a method (local in some languages). However, I&amp;#8217;ve never seen it used that way, and I&amp;#8217;d be a bit leery of doing it myself except in programs that are part of a project where all the developers are comfortable with the idiom, and where the code isn&amp;#8217;t expected to leave the project.&lt;/p&gt;


	&lt;p&gt;John Roth&lt;/p&gt;</description>
      <pubDate>Fri, 23 May 2008 11:04:56 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:5a6c73fe-01de-447d-94c9-5116a2f1f16b</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-1781</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by Tim</title>
      <description>&lt;p&gt;I&amp;#8217;ve not used it on purpose.  So far as I know, I&amp;#8217;ve not seen it used on purpose.  I&amp;#8217;ve accidentally used it a few times.   The tinker in me wants to try using it on purpose, and the professional refuses.  Yet another case of ambivalence.&lt;/p&gt;</description>
      <pubDate>Fri, 23 May 2008 07:33:35 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:1f457984-e4ee-4546-a836-009090adb24e</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-1780</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by Kevin</title>
      <description>&lt;p&gt;So is that link up at the top supposed to point back to this article? Just curious :-)&lt;/p&gt;


	&lt;p&gt;Kevin&lt;/p&gt;


&lt;blockquote&gt;Ooops.  Fixed now.  Thanks. &amp;#8212;tim&lt;/blockquote&gt;</description>
      <pubDate>Fri, 23 May 2008 03:33:54 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:948a6d94-d8f8-44f1-b84b-1bdb1f847866</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-1779</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by paddy3118</title>
      <description>&lt;p&gt;You get bit once, but have you seen anyone leave it in on purpose?&lt;/p&gt;


	&lt;p&gt;- Paddy.&lt;/p&gt;</description>
      <pubDate>Fri, 23 May 2008 01:10:34 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:c4bbe5db-4f12-45ff-a418-e3e5b56636e3</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-1778</link>
    </item>
  </channel>
</rss>
