Last active
April 12, 2019 12:49
-
-
Save an-dr/3c874181ccdcaee89d4f6115a0c66433 to your computer and use it in GitHub Desktop.
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
""" | |
Source: https://www.blog.pythonlibrary.org/2014/02/14/python-101-how-to-change-a-dict-into-a-class/ | |
""" | |
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":"8 inches", | |
"material":"rubber"} | |
ball = Dict2Obj(ball_dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment