Skip to content

Instantly share code, notes, and snippets.

@ardenn
Created October 5, 2018 18:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ardenn/76aa5653245388519a2edb690d8ed7ba to your computer and use it in GitHub Desktop.
Save ardenn/76aa5653245388519a2edb690d8ed7ba to your computer and use it in GitHub Desktop.
def convert_to_dict(obj):
"""
A function takes in a custom object and returns a dictionary representation of the object.
This dict representation includes meta data such as the object's module and class names.
"""
# Populate the dictionary with object meta data
obj_dict = {
"__class__": obj.__class__.__name__,
"__module__": obj.__module__
}
# Populate the dictionary with object properties
obj_dict.update(obj.__dict__)
return obj_dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment