def merge_dicts(d1: dict, d2: dict) -> dict:
"""
Update two dicts of dicts recursively,
if either mapping has leaves that are non-dicts,
the second's leaf overwrites the first's.
"""
# in Python 2, use .iteritems()!
for k, v in d1.items():
if k in d2:
# this next check is the only difference!
if all(isinstance(e, MutableMapping) for e in (v, d2[k])):
d2[k] = merge_dicts(v, d2[k])
if isinstance(v, list):
d2[k].extend(v)
# we could further check types and merge as appropriate here.
d3 = d1.copy()
d3.update(d2)
return d3
评论列表
文章目录