This is surprising me a bit, as I had expected that the default lst
attribute in the instances of the class below are fresh list objects, and not the same one! I know if I move the initialization of the attribute to the body of the init this problem seems to be solved, but I still would like to understand what the point behind this design decision has been, since I am learning Python3.
class C:
def __init__(self, lst=list()):
self.lst = lst
c1=C()
c2=C()
c3=C()
print(id(c1.lst) == id(c2.lst) == id(c3.lst))
The above code returns (to my astonishment!) True
.