Python class and instance attribute confusion
<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>For class</p>

<pre><code>class ValidationResult():
    def __init__(self, passed=True, messages=[], stop=False):
        self.passed = passed
        self.messages = messages
        self.stop = stop
</code></pre>

<p>running </p>

<pre><code>foo = ValidationResult()
bar = ValidationResult() 
foo.messages.append("Foos message")  
print foo.messages
print bar.messages
</code></pre>

<p>produces </p>

<pre><code>['Foos message']
['Foos message']
</code></pre>

<p>yet this</p>

<pre><code>foo = ValidationResult()
bar = ValidationResult(messages=["Bars message"]) 
foo.messages.append("Foos message")  
print foo.messages
print bar.messages
</code></pre>

<p>produces</p>

<pre><code>['Foos message']
['Bars message']
</code></pre>

<p>I think I've missed the boat on understanding instance attributes here. In the first sample, what I expected was <code>Foos message</code> to only be applied to <code>foo</code>. What is the correct way to declare an object attribute only mutable by its instance?</p>

<p>Using Python 2.7.1</p>
