我有一个班级(self.d1)中声明的字典。 调用F1功能后,self.d1必须更新到本地字典里面声明F1Python - 更新班级功能中的班级自我词典
import copy
class num:
def __init__(self):
self.d1 = {'a':1, 'b':2, 'c':3}
self.f1(self.d1)
print self.d1
def f1(self,d):
d2 = {'d':4, 'e':5, 'f':6}
d = copy.deepcopy(d2)
test = num()
我期望的输出是:
{'d':4, 'e':5, 'f':6}
但输出
{'a':1, 'b':2, 'c':3}
我想了解问题所在,而不仅仅是解决方案
所以有反正改变原来的字典d被提到? – 2015-02-11 12:49:55
是的 - 请参阅@PM-2Ring的答案 – 2015-02-11 12:54:25