Overriding Dictionary Object

Adonis adonisv at REMTHISearthlink.net
Mon Jul 26 17:38:43 CEST 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.



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

import cPickle

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

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

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

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

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

         self._filename = filename

More information about the Python-list mailing list