<分区> 分区>
我有一个这种结构的设置字典:
main_dict = {
'a': {
'a1': 1,
'a2': 2,
},
'b': {
'bb': {
'bb1' : 1,
'bb2' : 2,
},
},
}
然后我有一些类保存对 main_dict 中包含的字典的引用,例如:
class B:
def __init__(self, settings):
self.settings = settings
my_b = B(main_dict['b'])
assert(my_b.settings is main_dict['b'])
所以我可以更新 main_dict 中的不可变值,这些更新将反射(reflect)在 my_b 中,因为 my_b.settings 是 main_dict['b'].
但是,我现在有一个新的根字典,其中的新设置遵循相同的结构:
new_dict = {
'a': {
'a1': 11,
'a2': 22,
},
'b': {
'bb': {
'bb1' : 11,
'bb2' : 22,
},
},
}
是否有一种简单且通用的方法可以将 new_dict 中的所有不可变值复制到 main_dict 中,从而使 my_b 中的引用保持不变?
