【发布时间】:2016-03-20 06:45:12
【问题描述】:
假设我有一个名为 Test 的类,其属性为 items。然后我创建了一个名为Best 的子类。其中有一个修改类属性项的方法。但它甚至修改了Test 的items,而我只为Best 修改items。
class Test():
items = []
class Best(Test):
def method(self):
type(self).items.append("a test")
>>> Best().method()
>>> Best.items
["a test"]
>>> Test.items
["a test"] # This is what I don't want.
【问题讨论】:
-
Best是否应该继承自Test? -
是的,我没有测试我的示例代码,抱歉。
标签: python class python-3.x inheritance