在下面的示例中,我希望 deepcopy 创建字段的副本,而不仅仅是复制引用。这里发生了什么,有没有简单的方法解决它?
from copy import deepcopy
class Test:
field = [(1,2)]
t1 = Test()
t2 = deepcopy(t1)
t2.field[0]=(5,10)
print t1.field # [(1,2)] expected but [(5,10)] obtained
print t2.field # [(5,10)] expected
输出:
[(5, 10)]
[(5, 10)]