pickle question: sequencing of operations

Russell E. Owen rowen at uw.edu
Tue May 8 15:19:35 EDT 2012


In article <rowen-DF116B.12542704052012 at news.gmane.org>,
 "Russell E. Owen" <rowen at uw.edu> wrote:

> What is the sequence of calls when unpickling a class with __setstate__?
> 
> >From experimentation I see that __setstate__ is called and __init__ is 
> not, but I think I need more info.
> 
> I'm trying to pickle an instance of a class that is a subclass of 
> another class that contains unpickleable objects.
> 
> What I'd like to do is basically just pickle the constructor parameters 
> and then use those to reconstruct the object on unpickle, but I'm not 
> sure how to go about this. Or an example if anyone has one.

The following seems to work, but I don't know why:
def __getstate__(self):
   ...return the argument dict needed for __init__

def __setstate__(self, argdict):
   self.__init__(**argdict)

-- Russell




More information about the Python-list mailing list