title: Python list should be empty on class instance initialisation, but it's not. Why?
body: <p>I would like to create instances of a class containing a list that's empty by default; instead of later setting this list to the final full list I would like to successively add items to it. Here's a piece of sample code illustrating this:</p>
  
  <pre><code>#!/usr/bin/python
  
  class test:
      def __init__(self, lst=[], intg=0):
          self.lista   = lst
          self.integer = intg
  
  name_dict = {}
  counter   = 0
  
  for name in ('Anne', 'Leo', 'Suzy'):
      counter += 1
  
      name_dict[name] = test()
      name_dict[name].integer += 1
      name_dict[name].lista.append(counter)
  
      print name, name_dict[name].integer, name_dict[name].lista
  </code></pre>
  
  <p>When I ran the above program I expected to get</p>
  
  <blockquote>
    <p>Anne 1 [1] <br>
    Leo 1 [2] <br>
    Suzy 1 [3] <br></p>
  </blockquote>
  
  <p>as I assumed <code>lista</code> to always be initialised to an empty list.</p>
  
  <p>What I got instead was this:</p>
  
  <blockquote>
    <p>Anne 1 [1] <br>
    Leo 1 [1, 2] <br>
    Suzy 1 [1, 2, 3] <br></p>
  </blockquote>
  
  <p>If I replace <code>self.lista = lst</code> by <code>self.lista = []</code> it works fine, just like when I add the line <code>name_dict[name].lista = []</code> to the for loop.</p>
  
  <p>Why is it that the contents of the previous objects' lists are retained, yet their values of <code>integer</code> aren't? I am rather new to Python, so it would be great if someone could point out to me where my thoughts/assumptions have gone astray.</p>
  
  <p>Thanks a lot in advance for your replies.</p>