I am confused why this happens. I'm defining a class Myclass
for testing.
class Myclass:
l = []
def method1(self):
self.l.append("test")
>>>a = Myclass()
>>>a.l
[]
>>>a.method1()
>>>a.l
['test']
>>>b = Myclass()
>>>b.l
['test']
Why does this happen? Why isn't a new empty
list initialized?
Then
class Myclass:
l = "string1"
def method1(self):
self.l = "string2"
>>>a = Myclass()
>>>a.l
'string1'
>>>a.method1()
>>>a.l
'string2'
b = Myclass()
>>>b.l
'string1'
Why does this happen for mutable
objects only?