Created
June 27, 2014 00:35
-
-
Save lricoy/607eb5bad0e8a065a625 to your computer and use it in GitHub Desktop.
dict_to_obj.py
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
#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