<?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 mobile software development </title>
      <description>&lt;p&gt;This is the perfect blog for anyone who wants to know about this topic. The article is nice and it is pleasant to read. I have known very important things over here.&lt;/p&gt;</description>
      <pubDate>Mon, 06 Feb 2012 07:06:18 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:63976e30-629b-4e7f-8c3a-d35cbb31fd70</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-201937</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by Career coaching</title>
      <description>&lt;p&gt;This is the perfect blog for anyone who wants to know about this topic. The article is nice and it is pleasant to read. I have known very important things over here.&lt;/p&gt;</description>
      <pubDate>Sat, 04 Feb 2012 00:20:41 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:d35e8a63-6809-4799-a7ce-f2383f056aaf</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-201681</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by fantakuchen</title>
      <description>&lt;p&gt;&amp;#8220;Totally agree with you dude&amp;#8230; And the best part is all the comments placed by the participants are really interesting and informative&amp;#8230; Thanks!!&lt;/p&gt;</description>
      <pubDate>Fri, 03 Feb 2012 06:48:27 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:76cc41d5-4e22-4c2f-bc0e-f2ef78aa800f</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-201573</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by ralph lauren</title>
      <description>&lt;p&gt;Hiver n&amp;#8217;est pas termin&#233;e, mais les grandes marques ont commenc&#233; le d&#233;but de ce printemps, Ralph Lauren est l&amp;#8217;un d&amp;#8217;entre eux. Marque sous le soleil printanier et s&#233;ries all&#233; &#224; los angeles plage en Californie, Etats-Unis, avec le th&#232;me de los angeles c&#244;te de los angeles Californie, los angeles s&#233;rie de femmes r&#233;partis en Californie du Nord avec not layout boh&#232;me et setting r&#233;tro, comme theme flower, robe en mousseline de soie corps mince et ainsi de range.&lt;a href="http://www.vetementpoloralphlauren2012.net" rel="nofollow"&gt;http://www.vetementpoloralphlauren2012.net&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 02 Feb 2012 02:57:28 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:5e2e23a6-f624-42d8-82a4-3aa9f869739e</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-200759</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by campervan in australia</title>
      <description>&lt;p&gt;Thank you very much for providing outstanding information, your site is just what i was looking for&amp;#8230;&lt;/p&gt;</description>
      <pubDate>Wed, 01 Feb 2012 06:51:49 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:24408472-be2f-40ad-aa64-d31742643109</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-200543</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by airlie beach accommodation</title>
      <description>&lt;p&gt;You gave nice ideas here. I done a research on the issue and learn most peoples will agree with your blog. Certainly, these practices are unfair; but they say that most of their rules are only to apply to people who overdraw.&lt;/p&gt;</description>
      <pubDate>Mon, 30 Jan 2012 22:29:26 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:79855a52-c941-4e31-ba5d-9ab50fbad571</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-200295</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by Binary Options Questions</title>
      <description>&lt;p&gt;I will always give a nice thrust look in to you from my bookmark feed. Thanks for sharing. It is interesting.&lt;/p&gt;</description>
      <pubDate>Mon, 30 Jan 2012 05:42:57 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:62f67a9b-76b8-4430-85ea-bfe193065293</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-200216</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by Aeronaves</title>
      <description>&lt;p&gt;A source of valuable information in an accessible form.&lt;/p&gt;</description>
      <pubDate>Sun, 29 Jan 2012 19:10:45 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:e30b7d62-2e6e-4ceb-bc9c-4a09deef43fc</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-200127</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by technical writing services</title>
      <description>&lt;p&gt;I am in fact happy to glance at this blog posts which contains plenty of useful facts, thanks for providing these kinds of statistics.&lt;/p&gt;</description>
      <pubDate>Fri, 27 Jan 2012 11:56:15 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:6fe4a84a-0b35-4ea2-8f8f-0566830e8c55</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-199367</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by coolum accommodation</title>
      <description>&lt;p&gt;Howdy. Very cool blog!! Guy .. Excellent .. Superb .. I&#8217;ll bookmark your web site and take the feedadditionally&#8230;I&#8217;m happy to locate so much helpful info here within the post. Thank you for sharing.&lt;/p&gt;</description>
      <pubDate>Wed, 25 Jan 2012 19:05:54 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:e1732cd9-e8e6-4d54-8691-831c63359155</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-199192</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by maroochydore accommodation</title>
      <description>&lt;p&gt;I must we appreciate you your time and efforts you earn in publishing this website post. I&amp;#8217;m hoping the same best article by you later on also. Actually your creative writing expertise has encouraged me to start out my own blog site now. Actually the blogging is spreading its wings quickly. Your article can be a fine style of it.&lt;/p&gt;</description>
      <pubDate>Wed, 25 Jan 2012 17:41:04 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:30efa2c5-2948-421c-926a-1e88c1a73e8c</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-199191</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by Port Douglas Accommodation</title>
      <description>&lt;p&gt;I beleive this research helped to understand why one may lose weight faster or slower than another. All this computer simulations are very good way of research.&lt;/p&gt;</description>
      <pubDate>Wed, 25 Jan 2012 10:28:03 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:e5a3da69-bbf2-4bb1-bbc8-7fa28d199ffc</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-199168</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by coolangatta accommodation</title>
      <description>&lt;p&gt;This is the first time i am reading your post and admire that you posted article which gives users lot of information regarding particular topic thnaks for this share.&lt;/p&gt;</description>
      <pubDate>Wed, 25 Jan 2012 01:21:05 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:277763e1-4c70-4d60-ab93-ed340464c344</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-199097</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by Port Douglas Accommodation</title>
      <description>&lt;p&gt;Yes, I have found that my husband can lose weight easier than I can. I would consider myself an average weight for my age and height but it still becomes discouraging that it is easier for others to lose weight. It is a lifetime change and I that is an interesting point about permanently cutting out 10 calories a day. Thanks for posting.&lt;/p&gt;</description>
      <pubDate>Tue, 24 Jan 2012 16:06:11 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:c35af434-703d-4cda-b3ac-6f10e9b752ec</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-199069</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by airlie beach accommodation</title>
      <description>&lt;p&gt;First of all let me tell you, you have got a great blog .I am interested in looking for more of such topics and would like to have further information. Hope to see the next blog soon.&lt;/p&gt;</description>
      <pubDate>Tue, 24 Jan 2012 02:43:56 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:660b457a-c4f7-47e9-ad6f-bd8189cb3338</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-199001</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by maroochydore accommodation</title>
      <description>&lt;p&gt;Its my great pleasure to visit your site and to enjoy your awesome post here. I like it very much. I can feel that you put much attention for these articles, as all of them make sense and are very useful.&lt;/p&gt;</description>
      <pubDate>Mon, 23 Jan 2012 14:01:19 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:0711d504-e528-479b-a80b-09a87df1b2a9</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-198943</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by maroochydore accommodation</title>
      <description>&lt;p&gt;am delighted today for the first time you have.A source of valuable information in an accessible form.&lt;/p&gt;</description>
      <pubDate>Mon, 23 Jan 2012 12:03:21 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:b333b338-3e8d-4f99-9e92-c3622ea125e0</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-198932</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by sunshine beach accommodation</title>
      <description>&lt;p&gt;I like the valuable info you provide in your articles. I&#8217;ll bookmark your blog and check again here often. I am quite positive I will learn plenty of new stuff right here!&lt;/p&gt;</description>
      <pubDate>Thu, 19 Jan 2012 01:05:17 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:374686c4-3270-4727-a7c6-3be861192097</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-198553</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by I will surely share this with my friends. Your are really done great job, You style of working is amazing. keep it up dear.</title>
      <description>&lt;p&gt;I will surely share this with my friends. Your are really done great job, You style of working is amazing. keep it up dear.&lt;/p&gt;</description>
      <pubDate>Sat, 14 Jan 2012 05:42:56 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:f2fd55c6-d909-4547-9503-362531cf609a</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-197830</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by law school personal statement</title>
      <description>&lt;p&gt;Very good work on such topic. When I started to read article I was thinking about the way you present such great topic. By the way could you please post more.&lt;/p&gt;</description>
      <pubDate>Sat, 07 Jan 2012 02:38:17 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:1bfa5ad0-6928-4382-a65c-81375f8b1173</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-195835</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by NH Wedding Reception</title>
      <description>&lt;p&gt;I actually do not face such kind of problem. i am keeping it in my dairy for further use actually. I hope this will help me a lot.&lt;/p&gt;</description>
      <pubDate>Fri, 06 Jan 2012 21:51:41 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:c726e749-456c-4a7a-adcc-8c4e27f5b1c6</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-195694</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by Games For Young Children</title>
      <description>&lt;p&gt;Robert Martin&amp;#8217;s keynote at Agile2008 and your previous post on pairing brought the topic to the front of my mind recently and it&amp;#8217;s nice to see some real data on the subject. It&amp;#8217;s nice to see a trend of unreduced productivity with better defect rates across most of the studies. Definitely a lot of food for thought and further reading here.&lt;/p&gt;</description>
      <pubDate>Thu, 05 Jan 2012 11:46:17 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:f241587b-be40-4c94-b90c-de0fc3b9dd40</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-194927</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by http://www.thebettercleanse.com</title>
      <description>&lt;p&gt;The blog was absolutely fantastic. Lots of great information which can be helpful in some or the other way.&lt;/p&gt;</description>
      <pubDate>Thu, 29 Dec 2011 05:20:38 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:686ac437-6af6-4c6c-a4af-5653d4aa0913</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-193209</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by Laser tattoo removal Gold Coast</title>
      <description>&lt;p&gt;All the contents you mentioned in post are too good and can be very useful. This one is great and is really a good post. I think it will help me a lot in the related stuff and is very much useful for me. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.&lt;/p&gt;</description>
      <pubDate>Wed, 28 Dec 2011 04:07:20 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:d45f85b3-e2bb-4ab6-96c7-c2c317db8e62</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-192924</link>
    </item>
    <item>
      <title>"Python's Mutable Default Problem" by gucci scarf</title>
      <description>&lt;p&gt;speaking bad languages&lt;/p&gt;</description>
      <pubDate>Fri, 23 Dec 2011 20:51:59 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:de7f6ebc-b4dd-4a97-840d-fe39afd3a807</guid>
      <link>http://blog.objectmentor.com/articles/2008/05/22/pythons-mutable-default-problem#comment-190941</link>
    </item>
  </channel>
</rss>

