這應該是簡單的...防止成員對象引用相同的列表
class Object:
x = 0
y = []
a = Object()
b = Object()
a.x = 1
b.x = 2
print a.x, b.x
# output: 1 2
# works as expected
a.y.append(3)
b.y.append(4)
print a.y, b.y
# output: [3, 4] [3, 4]
# same list is referenced how to fix?
# desired output: [3] [4]
據我所知,a.y
和b.y
引用相同的列表。我怎樣才能讓他們分開?優選地,不需要增加方法。
打印斧頭,B,Y的應該打印這個:1 [] –
這是'__init__'的用途。你爲什麼不想用它? –
@gnibbler如果我確切知道Python對象如何工作我不會在這裏:) – devtk