Skip to content

Instantly share code, notes, and snippets.

@timofurrer
Created August 19, 2013 12:03
Show Gist options
  • Save timofurrer/6268402 to your computer and use it in GitHub Desktop.
Save timofurrer/6268402 to your computer and use it in GitHub Desktop.
simple base object where you can inherit from and your object can be created from a dictionary. This can be useful if you want to convert some data from JSON or YAML to an object.
# -*- coding: utf-8 -*-
class DictObj(object):
def load(self, **data):
self.__dict__.update(data)
@classmethod
def create(cls, **data):
s = cls()
s.load(**data)
return s
class Student(DictObj):
pass
data = {"name": "Max", "age": 20}
s = Student.create(**data)
print "Name is: ", s.name
print "Age is: ", s.age
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment