Optional argument keeps pointing to same value
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument">“Least Astonishment” in Python: The Mutable Default Argument</a>  </p>
</blockquote>



<p>I'm trying to write a function that randomizes a list in Python 2.7.3. The code that does it is this:</p>

<pre><code>import random

def randomize_list(l, new=[]):
    if not len(l):
        return new

    print id(l), id(new)

    l1 = list(l)

    new.append(l1.pop(
        random.choice(list(range(len(l1))))
    ))

    return randomize_list(l1, new)
</code></pre>

<p>If I import this function, and call it multiple time, the <code>id(new)</code>, always returns the same value, and each subsequent call to <code>randomize_function</code> adds items to the results of previous invocation. So, for example:</p>

<pre><code>&gt;&gt;&gt; a = [1,2,3,4]
&gt;&gt;&gt; randomize_list(a)
35192344 35234016
16126176 35234016
16126536 35234016
16126896 35234016
[2,3,4,1]
&gt;&gt;&gt; randomize_list(a)
35192344 35234016
35193488 35234016
35193344 35234016
16126608 35234016
[2,3,4,1,3,2,1,4]
... and so on
</code></pre>

<p>Why is this happening, and how do I fix it?</p>
