Skip to content

Instantly share code, notes, and snippets.

@lricoy
Created June 27, 2014 00:35
Show Gist options
  • Save lricoy/607eb5bad0e8a065a625 to your computer and use it in GitHub Desktop.
Save lricoy/607eb5bad0e8a065a625 to your computer and use it in GitHub Desktop.
dict_to_obj.py
#coding: utf-8
class Dict2Obj(object):
"""
Turns a dictionary into a class
"""
def __init__(self, dictionary):
"""Constructor"""
for key in dictionary:
setattr(self, key, dictionary[key])
def __repr__(self):
""""""
attrs = str([ x for x in self.__dict__])
return "<Dict2Obj: %s" % attrs
if __name__ == "__main__":
ball_dict = {"color": "blue",
"size": 10,
"material": "rubber",}
ball = Dict2Obj(ball_dict)
print(ball)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment