Created
April 6, 2015 23:51
-
-
Save arthurio/70a6562a2f8fb5725b77 to your computer and use it in GitHub Desktop.
Python function to merge 2 dicts, similar to what angular does
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Extends a dict object A with the properties of a dict object B. | |
@param a: dict object | |
@param b: dict object | |
''' | |
def extends(a, b): | |
for key in b: | |
if key in a: | |
if isinstance(a[key], dict) and isinstance(b[key], dict): | |
a[key] = extends(a[key], b[key]) | |
elif a[key] == b[key]: | |
pass # same leaf value | |
else: | |
a[key] = b[key] | |
else: | |
a[key] = b[key] | |
return a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment