How to create a Persistable Dictionary in Python

I came across a situation where I had to implement a Python class which can;

  • handle dynamic attributes
  • can be initialized with a dictionary
  • can be persisted on the local storage as a file
  • can be used as a dictionary as well as a object. ( allow both  foo['key']  and  foo.key  to access its attributes )

So I came up with this Class. It inherits from python’s built in Dictionary class ( dict ). Therefore it can be initialized with an existing dictionary.  And it also overrides  __getattr__   __setattr__  __delattr__ . Therefore it can handle dynamic attributes and act as a dictionary as well as a object. Finally It uses Python’s cPickle to serialize and persist it self in the local storage.

Please share your thoughts on this approach. 🙂