boltons
boltons copied to clipboard
dict rupdate()
def recursive_update(a, b):
'dict.update(), but recurse when both values are dicts'
for k, v in b.items():
if v.__class__ is dict and a.get(k).__class__ is dict:
a[k] = _recursive_update(a[k], v)
else:
a[k] = b[k]
return a
Simple easy to understand idea I think. Tricky enough that people are messing it up though: http://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth
simple enough that it could perhaps use stack-of-dicts instead of recursion
Potentially duplicate of https://github.com/mahmoud/boltons/issues/81, Or maybe just touches https://github.com/mahmoud/boltons/issues/81#issuecomment-249414209?
Oh, that's a good point!
This might just be a special case of remerge([dict_a, dict_b]).
If remerge is available, maybe a better way to handle this would be as a documented use-case of remerge.
def recursive_update(a, b):
return remerge([a, b])