Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DictObj(dict):
- def __init__(self, **kwargs):
- dict.__init__(self, **kwargs)
- self.__dict__ = self
- obj = DictObj(a=1)
- print(obj.a) # 1
- print(obj.get('a')) # 1
- print(obj) # {'a': 1}
- obj.a = 2
- print(obj.a) # 2
- print(obj.get('a')) # 2
- print(obj) # {'a': 2}
- obj.b = 3
- print(obj.b) # 3
- print(obj.get('b')) # 3
- print(obj) # {'a': 2, 'b': 3}
- obj.update({'c': 4})
- print(obj.c) # 4
- print(obj.get('c')) # 4
- print(obj) # {'a': 2, 'b': 3, 'c': 4}
- new_obj = DictObj(**{'foo': 'bar'})
- print(new_obj.foo) # bar
- print(new_obj.get('foo')) # bar
- print(new_obj) # {'foo': 'bar'}
Add Comment
Please, Sign In to add comment