Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
Can anyone explain the following strange behaviour?
I have the following class:
class Zoo:
def __init__(self,alist=[]):
self.animals = alist
def __len__(self):
return len(self.animals)
def add(self,a):
self.animals.append(a)
and when I do the following,
In [38]: z=Zoo()
In [39]: z.add(2)
In [40]: z.add(23)
In [41]: len(z)
Out[41]: 2
In [42]: z2=Zoo()
In [43]: len(z2)
Out[43]: 2
Why is z2.animals not an empty list?
Thanks, Matthias