Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
I am not sure what this is called, and thus have had difficulties finding documentation for what is going on. I was hoping someone here might be able to point me in the right direction.
In the following example, where I have a list as a default value for a keyword argument, it behaves in a way I was not expecting:
class A(object):
def __init___(self, c=[0,0]):
self.c = c
class B(A):
def __init__(self):
super(B, self).__init__()
a = A()
b = B()
print a.c, b.c # outputs [0, 0] [0, 0]
a.c[1] = 5
print a.c, b.c # outputs [0, 5] [0, 5]
I understand that lists are mutable, but I had assumed that in the case of using a list as a default keyword argument, a 'new' list would be created each time. Is there documentation explaining why this is not the case?