Skip to content

Instantly share code, notes, and snippets.

@octaflop
Created October 3, 2017 21:09
Show Gist options
  • Save octaflop/1b12120e3f5ad0273107ead33fad67fc to your computer and use it in GitHub Desktop.
Save octaflop/1b12120e3f5ad0273107ead33fad67fc to your computer and use it in GitHub Desktop.
Convert a python dictionary to an object
class asobj(object):
"""
Convert a python dictionary to an object accessible as attributes.
Args:
d: a python dictionary
Returns:
object: a python object with keys as attributes
"""
def __init__(self, d):
for key, val in d.iteritems():
if isinstance(val, dict):
d[key] = asobj(val)
self.__dict__ = d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment