Pickling and inheritance are making me hurt

Kirk Strauser kirk at strauser.com
Fri Feb 4 16:48:52 EST 2005


I have a module that defines a Search class and a SearchResult class.  I use
these classes by writing other modules that subclass both of them as needed
to interface with particular search engines.

My problem is that Search defines a method (called automatically by __del__)
to save its results between invocations:

    def _saveresults(self):
        self._oldresults = self._results
        file = open(self._storefile(), 'w')
        pickle.dump(self._oldresults, file)
        file.close()

The problem I'm having is the the pickle.dump call is failing whenever the
objects in "self.data" are instances of derivatives of SearchResult rather
than instances of SearchResult itself (which is pretty much always the
case):

    Exception pickle.PicklingError: <pickle.PicklingError instance at
0xb7f7ad6c> in <bound method Search.__del__ of <__main__.Search object at
0xb7ec954c>> ignored


Now, if I overload _saveresults inside a subclass of Search, then it works. 
It seems like the problem is that _saveresults is only looking inside the
same namespace as Search (where it's originally defined), even if it's
actually been inherited by another class in a different module.  Is there a
way around this?

-- 
Kirk Strauser



More information about the Python-list mailing list