Advertisement
renix1

Acessing items with dot on python

Nov 4th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.37 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. person = {"name": "Reni", "age": 19}
  4.  
  5. class MyDict(dict):
  6.     def __init__(self, std_dict):
  7.         self.myself = std_dict
  8.         self.__construct_obj()
  9.        
  10.     def __construct_obj(self):
  11.         for key in self.myself.keys():
  12.             setattr(self, key, self.myself[key])
  13.        
  14. try:
  15.     print(person.name)
  16. except Exception as e:
  17.     print(e)
  18.    
  19. p = MyDict(person)
  20. print(p.name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement