test1 = dumby() # ok, you create a new dumby
A = [1,1] # ok you create a new list referenced by A
test1.a = A # test1.a now references the same list
print(test1.a)
A.remove(A[0]) # the list is modified
print(test1.a) # you can control that the list is modified through the other ref.
您要做的是分配原始列表的副本:
test1.a = A[:] # test1.a receives a copy of A (an independent object)
Python变量(或成员属性)实际上包含对对象的引用。有些对象是不可变的(数字、字符串),但大多数列表是不可变的。所以当你修改一个可变的对象时,所有对它的引用都会受到影响,不管用什么引用来改变它。你知道吗
这正是这里发生的事情:
您要做的是分配原始列表的副本:
相关问题 更多 >
编程相关推荐