As it turns out,
listvar += list
is interpreted as adding the elements of list
to the existing list referenced by listvar
, while
listvar = listvar + list
is interpreted as creating a new list which is the concatenation of listvar
and list
, and then storing that new list in listvar
- overwriting the reference to the previous list.
Hence why you get different results - in the first case, self.bar is shared by both classes, and thus the init functions for both add to the same list, because no new list is ever created. In the second case, each initializer creates a new list, and thus f and g wind up with different lists stored.
An easy way to see this is the following:
a = [1]
b = a
c = a
b = b + [2]
c += [3]
print a,b,c # Results in [1, 3] [1, 2] [1, 3]