我正在尝试理解Python中浅复制和深复制之间的区别。我在这里阅读了许多帖子,它们很有帮助。然而,我仍然不太清楚它们之间的区别。请问,有人能否解释下面结果的原因。我不理解的结果在注释中指出。
非常感谢。
非常感谢。
import copy
import random
class understand_copy(object):
def __init__(self):
self.listofvalues = [4, 5]
def set_listofvalues(self, pos, value):
self.listofvalues[pos] = value
ins = understand_copy()
newins = copy.copy(ins)
newins.set_listofvalues(1,3)
print "ins = ", ins.listofvalues
print "in newins", newins.listofvalues
# Gives the following output as I would expect based on the explanations.
# prints ins = [4, 3]
# prints newins = [4, 3]
newins.listofvalues.append(5)
print "ins =", ins.listofvalues
print "newins =", newins.listofvalues
# Gives the following output as I would expect based on the explanations.
# prints ins = [4, 3, 5]
# prints newins = [4, 3, 5]
newins.listofvalues = [10, 11]
print "ins = ", ins.listofvalues
print "newins = ", newins.listofvalues
# Gives
# ins = [4, 3, 5]
# newins = [10, 11]
# This is the output that I do not understand.
# Why does ins.listofvalues not change this case.**