What does plus equals (+=) do in Python?
<p>My friend sent me the following Python code to demonstrate what she thinks is wrong with Python. At the moment, I have to agree with her. All of the experienced programmers I've shown this to are equally confused. Can anyone tell me what is going on?  </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>foo += bar seems to affect every instance of the class, whereas foo = foo + bar seems to behave in the way I would expect things to behave. </p>

<p>I've tried googling this, but I'm not sure what the '+=' operator is officially called, so haven't been able to find anything.</p>
