Skip to content

Instantly share code, notes, and snippets.

@an-dr
Last active April 12, 2019 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save an-dr/3c874181ccdcaee89d4f6115a0c66433 to your computer and use it in GitHub Desktop.
Save an-dr/3c874181ccdcaee89d4f6115a0c66433 to your computer and use it in GitHub Desktop.
"""
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