Python class function default variables are class objects?
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument">&ldquo;Least Astonishment&rdquo; in Python: The Mutable Default Argument</a>  </p>
</blockquote>



<p>I was writing some code this afternoon, and stumbled across a bug in my code. I noticed that the default values for one of my newly created objects was carrying over from another object! For example:</p>

<pre><code>class One(object):
    def __init__(self, my_list=[]):
        self.my_list = my_list

one1 = One()
print(one1.my_list)
[] # empty list, what you'd expect.

one1.my_list.append('hi')
print(one1.my_list)
['hi'] # list with the new value in it, what you'd expect.

one2 = One()
print(one2.my_list)
['hi'] # Hey! It saved the variable from the other One!
</code></pre>

<p>So I know it can be solved by doing this:</p>

<pre><code>class One(object):
    def __init__(self, my_list=None):
        self.my_list = my_list if my_list is not None else []
</code></pre>

<p>What I would like to know is... Why? Why are Python classes structured so that the default values are saved across instances of the class?</p>

<p>Thanks in advance!</p>
