Guest User

Untitled

a guest
Mar 19th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. class DictObj(dict):
  2. def __init__(self, **kwargs):
  3. dict.__init__(self, **kwargs)
  4. self.__dict__ = self
  5.  
  6. obj = DictObj(a=1)
  7. print(obj.a) # 1
  8. print(obj.get('a')) # 1
  9. print(obj) # {'a': 1}
  10.  
  11. obj.a = 2
  12. print(obj.a) # 2
  13. print(obj.get('a')) # 2
  14. print(obj) # {'a': 2}
  15.  
  16. obj.b = 3
  17. print(obj.b) # 3
  18. print(obj.get('b')) # 3
  19. print(obj) # {'a': 2, 'b': 3}
  20.  
  21. obj.update({'c': 4})
  22. print(obj.c) # 4
  23. print(obj.get('c')) # 4
  24. print(obj) # {'a': 2, 'b': 3, 'c': 4}
  25.  
  26. new_obj = DictObj(**{'foo': 'bar'})
  27. print(new_obj.foo) # bar
  28. print(new_obj.get('foo')) # bar
  29. print(new_obj) # {'foo': 'bar'}
Add Comment
Please, Sign In to add comment