Using Python 2.7, we expect the following code,
class T(object):
def __init__(self,x = list()):
self.x = x
self.x.append(0)
print self.x
a = T()
b = T()
to print
[0]
[0]
However, what actually happens is the following
[0]
[0, 0]
I am unable to spot why this happen, and how I can prevent this behaviour. It is clear, that [] in the default parameter suddenly becomes a reference to an object shared by all instances of the class... But why? And how can one prevent this?