这里是一部法典:
>>> class A(object):
... value = []
... def method(self, new_value):
... self.value.append(new_value)
...
>>> a = A()
>>> a.value
[]
>>> a.method(1)
>>> b = A()
>>> b.value
[1]
>>> b.method(2)
>>> b.value
[1, 2]
>>> a.value
[1, 2]
This happens only with lists. Is the only way to deffine value in __init__?
How to normally define default class values in python?
<><>UPD>/strong>
感谢你的答复
>>> class B(object):
... value = "str"
... def method(self):
... self.value += "1"
...
>>> a = B()
>>> a.value
str
>>> a.method()
>>> a.value
str1
>>> b = B()
>>> b.value
str
我看不出,为什么要分享名单,但又不是?