Skip to content

Instantly share code, notes, and snippets.

@arthurio
Created April 6, 2015 23:51
Show Gist options
  • Save arthurio/70a6562a2f8fb5725b77 to your computer and use it in GitHub Desktop.
Save arthurio/70a6562a2f8fb5725b77 to your computer and use it in GitHub Desktop.
Python function to merge 2 dicts, similar to what angular does
'''
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