pickle question: sequencing of operations

Ian Kelly ian.g.kelly at gmail.com
Tue May 8 15:55:05 EDT 2012


On Tue, May 8, 2012 at 1:19 PM, Russell E. Owen <rowen at uw.edu> wrote:
> 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__?

I believe it just calls object.__new__ followed by
yourclass.__setstate__.  So at the point __setstate__ is called, you
have a pristine instance of whatever class you're unpickling.

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

That seems like it should be fine as long as all your initialization
code is in __init__ and not in __new__.

Cheers,
Ian



More information about the Python-list mailing list