I have a dictionary of settings of this kind of structure:
main_dict = {
'a': {
'a1': 1,
'a2': 2,
},
'b': {
'bb': {
'bb1' : 1,
'bb2' : 2,
},
},
}
I then have some classes which hold references to the dictionaries contained within main_dict
, such as like this:
class B:
def __init__(self, settings):
self.settings = settings
my_b = B(main_dict['b'])
assert(my_b.settings is main_dict['b'])
So I can update the immutable values within main_dict
and these updates will be reflected in my_b
because my_b.settings is main_dict['b']
.
However, I now have a new root dictionary with new settings in it following the same structure:
new_dict = {
'a': {
'a1': 11,
'a2': 22,
},
'b': {
'bb': {
'bb1' : 11,
'bb2' : 22,
},
},
}
Is there a simple and general purpose way to copy all of the immutable values in new_dict
into main_dict
such that the reference in my_b will be left intact?