Skip to content

Instantly share code, notes, and snippets.

@byron2r
Created May 11, 2012 03:54
Show Gist options
  • Save byron2r/2657437 to your computer and use it in GitHub Desktop.
Save byron2r/2657437 to your computer and use it in GitHub Desktop.
python dict2object
class obj(object):
def __init__(self, d):
for a, b in d.items():
if isinstance(b, (list, tuple)):
setattr(self, a, [obj(x) if isinstance(x, dict) else x for x in b])
else:
setattr(self, a, obj(b) if isinstance(b, dict) else b)
>>> x = obj(d)
>>> x.b.c
2
>>> x.d[1].foo
'bar'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment