Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
Default values for arguments
I'm having trouble explaining how a list belonging to a class behaves.
>>> class A(object):
... def __init__(self, L=[]):
... self.L = L
...
>>> a1 = A()
>>> a2 = A()
>>> a1.L.append("test")
>>> print a1.L
['test']
>>> print a2.L
['test']
In this example, how did a2.L get a value in it? I only added an item to the list in a1. How is it that they're sharing the list now.
Thanks,
Henry