title: Python class constructor with default arguments
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://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>Can anyone explain the following strange behaviour?</p>
  
  <p>I have the following class:</p>
  
  <pre><code>class Zoo:
      def __init__(self,alist=[]):
          self.animals = alist
  
      def __len__(self):
          return len(self.animals)
  
      def add(self,a):
          self.animals.append(a)
  </code></pre>
  
  <p>and when I do the following,</p>
  
  <pre><code>In [38]: z=Zoo()
  In [39]: z.add(2)
  In [40]: z.add(23)
  In [41]: len(z)
  Out[41]: 2
  
  In [42]: z2=Zoo()
  
  In [43]: len(z2)
  Out[43]: 2
  </code></pre>
  
  <p>Why is z2.animals not an empty list?</p>
  
  <p>Thanks, Matthias</p>