title: Why does += behave unexpectedly on lists?
body: <p>The <code>+=</code> operator in python seems to be operating unexpectedly on lists.  Can anyone tell me what is going on here?</p>
  
  <pre><code>class foo:  
       bar = []
       def __init__(self,x):
           self.bar += [x]
  
  
  class foo2:
       bar = []
       def __init__(self,x):
            self.bar = self.bar + [x]
  
  f = foo(1)
  g = foo(2)
  print f.bar
  print g.bar 
  
  f.bar += [3]
  print f.bar
  print g.bar
  
  f.bar = f.bar + [4]
  print f.bar
  print g.bar
  
  f = foo2(1)
  g = foo2(2)
  print f.bar 
  print g.bar 
  </code></pre>
  
  <p><strong>OUTPUT</strong></p>
  
  <pre><code>[1, 2]
  [1, 2]
  [1, 2, 3]
  [1, 2, 3]
  [1, 2, 3, 4]
  [1, 2, 3]
  [1]
  [2]
  </code></pre>
  
  <p><code>foo += bar</code> seems to affect every instance of the class, whereas <code>foo = foo + bar</code> seems to behave in the way I would expect things to behave. </p>
  
  <p>The <code>+=</code> operator is called a "compound assignment operator". </p>