<story><title>Python quirks</title><url>http://www.lshift.net/blog/2009/10/29/python-quirks</url><text></text></story><parent_chain><item><author>emef</author><text>I&amp;#x27;m surprised using mutable value for a default in a keyword argument wasn&amp;#x27;t mentioned. This certainly tripped me up early in learning python:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    def fn(x, my_dict={}):
        my_dict[x] = x * 2
        return my_dict

    &amp;gt;&amp;gt;&amp;gt; fn(1)
    {1: 2}
    &amp;gt;&amp;gt;&amp;gt; fn(2)
    {1: 2, 2: 4}
&lt;/code&gt;&lt;/pre&gt;
(I would have expected the second call to return {2: 4} when I was learning python)</text></item></parent_chain><comment><author>jliechti1</author><text>This was discussed pretty recently in this thread on &amp;quot;Python Newbie Mistakes, Part 1&amp;quot;.&lt;p&gt;&lt;i&gt;Default values for functions in Python are instantiated when the function is defined, not when it’s called.&lt;/i&gt;&lt;p&gt;Thread: &lt;a href=&quot;https://news.ycombinator.com/item?id=5999772&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;news.ycombinator.com&amp;#x2F;item?id=5999772&lt;/a&gt;&lt;p&gt;Direct link: &lt;a href=&quot;http://blog.amir.rachum.com/post/54770419679/python-common-newbie-mistakes-part-1&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;blog.amir.rachum.com&amp;#x2F;post&amp;#x2F;54770419679&amp;#x2F;python-common-n...&lt;/a&gt;&lt;p&gt;It looks like the author posted a &amp;quot;part 2&amp;quot; to HN as well, but it never made the front page.&lt;p&gt;&lt;a href=&quot;http://blog.amir.rachum.com/post/55024295793/python-common-newbie-mistakes-part-2&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;blog.amir.rachum.com&amp;#x2F;post&amp;#x2F;55024295793&amp;#x2F;python-common-n...&lt;/a&gt;</text></comment>
<story><title>Python quirks</title><url>http://www.lshift.net/blog/2009/10/29/python-quirks</url><text></text></story><parent_chain><item><author>emef</author><text>I&amp;#x27;m surprised using mutable value for a default in a keyword argument wasn&amp;#x27;t mentioned. This certainly tripped me up early in learning python:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    def fn(x, my_dict={}):
        my_dict[x] = x * 2
        return my_dict

    &amp;gt;&amp;gt;&amp;gt; fn(1)
    {1: 2}
    &amp;gt;&amp;gt;&amp;gt; fn(2)
    {1: 2, 2: 4}
&lt;/code&gt;&lt;/pre&gt;
(I would have expected the second call to return {2: 4} when I was learning python)</text></item></parent_chain><comment><author>kyllo</author><text>This is pretty bizarre, and seems like it would cause a memory leak if you don&amp;#x27;t know what you&amp;#x27;re actually doing. You&amp;#x27;re allocating a my_dict object on the heap, in a field of the function object, so my_dict never goes out of scope and never gets garbage collected until the function fn itself does, right?&lt;p&gt;Whereas if you did this instead:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    def fn(x):
        my_dict={}
        my_dict[x] = x * 2
        return my_dict
&lt;/code&gt;&lt;/pre&gt;
You&amp;#x27;d get what you&amp;#x27;d expect--a new my_dict object gets created, returned, and then goes out of scope every time fn is called, so it would get garbage collected once there are no more references to that return value. (I think...)&lt;p&gt;(I don&amp;#x27;t know that much about how memory allocation and GC works in Python yet, just trying to learn!)</text></comment>