<?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 +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>
    <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 +0000</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 +0000</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 +0000</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 +0000</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 +0000</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 +0000</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 +0000</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>
