Advertisement
Guest User

Untitled

a guest
Mar 26th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. from UserDict import UserDict
  2. class RecursiveDict(UserDict):
  3.     def __init__(self,*args,**kwargs):
  4.         UserDict.__init__(self,*args,**kwargs)
  5.     def __getitem__(self,key):
  6.         if key in self.data:
  7.             return self.data[key]
  8.         else:
  9.             self.__setitem__(key,dict())
  10.             return self.data[key]
  11.         if hasattr(self.__class__, "__missing__"):
  12.             return self.__class__.__missing__(self, key)
  13.         raise KeyError(key)
  14.     def __setitem__(self, key, item):
  15.          self.data[key] = item
  16. my_dict = RecursiveDict()
  17. my_dict['P'] = 6
  18. my_dict['K']['D'] = 7
  19. print my_dict
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement