当我运行此代码时:
class corners():
def __init__(self, odw=0, connections=[]):
self.odw = odw
self.connections = connections
w = [corners() for i in range(9)]
print w[1].odw, w[2].odw, w[3].odw
print w[1].connections, w[2].connections, w[3].connections
w[1].odw = 1
w[3].odw = 1
w[1].connections.append(2)
w[1].connections.append(3)
w[2].connections.append(88)
print w[1].odw, w[2].odw, w[3].odw
print w[1].connections, w[2].connections, w[3].connections
答案是:
0 0 0
[] [] []
1 0 1
[2, 3, 88] [2, 3, 88] [2, 3, 88] // it should be [2, 3] [88] []
该属性的答案' odw'是正确的。但是:我不知道为什么属性'连接'当我仅更改特定对象时,对所有对象进行修改。例如,当我将新元素添加到列表w [1]时,它将添加到所有其他元素。为什么呢?
感谢您的帮助。