Pickling and inheritance are making me hurt

Christian Dieterich cdieterich at geosci.uchicago.edu
Fri Feb 4 23:16:47 EST 2005


On Dé hAoine, Feabh 4, 2005, at 15:48 America/Chicago, Kirk Strauser 
wrote:

> 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?

It's hard to tell exactly what's going on. I have only suggestions what 
you could try to verify.

There was a thread a few days ago, where it was concluded that garbage 
collecting happens in alphabetical order. Can't remember which one it 
was. You could temporarily rename self._result to self.z_result to 
check that.

Here's a gotcha that I had in a similar context. Well, it's logical but 
somehow unexpected. If you do for example
s = Search()
s = Search()
the calling sequence for the constructor and destructor is
__init__()
__init__()
__del__()
Maybe this interferes with pickling your first instance?

Hope this helps debugging,

Christian




More information about the Python-list mailing list