Created
August 19, 2013 12:03
-
-
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.
This file contains hidden or 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 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