Overriding Dictionary Object

Adonis adonisv at REMTHISearthlink.net
Mon Jul 26 11:38:43 EDT 2004


I am subclassing the dict object, and having it serialize itself to disk for
later access, but I am having some trouble with accessing the self._filename
attribute, when the object is first created it functions fine, but when I
access it on a later time, self._filename is not present, how can I go about
this knowing that when objects are serialized they are not initialized on
subsequent access? I was under the impression that everything in an objects
scope was serialized by pickle, or is my assumption wrong?

Any help is greatly appreciated.

Adonis

----

Error:
  File "data.py", line 9, in _sync
    data = open(self._filename, 'w')
AttributeError: 'Driver' object has no attribute '_filename'


Code:
import cPickle

class Driver(dict):
    def _sync(self):
        data = open(self._filename, 'w')
        cPickle.dump(self, data, -1)
        data.close()

    def __delitem__(self, key):
        dict.__delitem__(self, key)
        self._sync()

    def __setitem__(self, key, value):
        dict.__setitem__(self, key, value)
        self._sync()

    def __init__(self, filename="appweb.data"):
        dict.__init__(self)

        if os.path.exists(filename):
            data = open(filename)
            self = cPickle.load(data)
            data.close()

         self._filename = filename





More information about the Python-list mailing list