Re: pickle question: sequencing of operations

2012-05-09 Thread Russell E. Owen
In article 
calwzidk3e353cnuuqpwr-4rromx7c9dbzapawurern9uzyu...@mail.gmail.com,
 Ian Kelly ian.g.ke...@gmail.com wrote:

 On Tue, May 8, 2012 at 1:19 PM, Russell E. Owen ro...@uw.edu wrote:
  In article rowen-df116b.12542704052...@news.gmane.org,
   Russell E. Owen ro...@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.

I was wondering. I override __new__ (and __init__) to print messages and 
was quite surprised to only see __new__being called when the object was 
first created, not when it was being unpickled. But maybe there's 
something funny about my override that caused unpickle to ignore it and 
use the default version. I hope so. I can't see how the object could be 
constructed during unpickle without calling __new__. But that's one 
reason I was curious about the unpickling sequence of operations.

-- Russell

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pickle question: sequencing of operations

2012-05-09 Thread Ian Kelly
On Wed, May 9, 2012 at 1:39 PM, Russell E. Owen ro...@uw.edu wrote:
 I was wondering. I override __new__ (and __init__) to print messages and
 was quite surprised to only see __new__being called when the object was
 first created, not when it was being unpickled. But maybe there's
 something funny about my override that caused unpickle to ignore it and
 use the default version. I hope so. I can't see how the object could be
 constructed during unpickle without calling __new__. But that's one
 reason I was curious about the unpickling sequence of operations.

You're probably pickling with the default protocol.  Unpickling calls
an overridden __new__ method only if the pickle protocol is at least
2.  Using protocol 0 or 1, new-style class instances are constructed
with the base object.__new__ instead.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pickle question: sequencing of operations

2012-05-09 Thread Ian Kelly
On Wed, May 9, 2012 at 2:34 PM, Ian Kelly ian.g.ke...@gmail.com wrote:
 On Wed, May 9, 2012 at 1:39 PM, Russell E. Owen ro...@uw.edu wrote:
 I was wondering. I override __new__ (and __init__) to print messages and
 was quite surprised to only see __new__being called when the object was
 first created, not when it was being unpickled. But maybe there's
 something funny about my override that caused unpickle to ignore it and
 use the default version. I hope so. I can't see how the object could be
 constructed during unpickle without calling __new__. But that's one
 reason I was curious about the unpickling sequence of operations.

 You're probably pickling with the default protocol.  Unpickling calls
 an overridden __new__ method only if the pickle protocol is at least
 2.  Using protocol 0 or 1, new-style class instances are constructed
 with the base object.__new__ instead.

BTW, in case you're wondering where all this is documented, pull up
PEP 307 and read the sections Case 2 and Case 3.

Cheers,
Ian
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pickle question: sequencing of operations

2012-05-08 Thread Russell E. Owen
In article rowen-df116b.12542704052...@news.gmane.org,
 Russell E. Owen ro...@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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pickle question: sequencing of operations

2012-05-08 Thread Ian Kelly
On Tue, May 8, 2012 at 1:19 PM, Russell E. Owen ro...@uw.edu wrote:
 In article rowen-df116b.12542704052...@news.gmane.org,
  Russell E. Owen ro...@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
-- 
http://mail.python.org/mailman/listinfo/python-list


pickle question: sequencing of operations

2012-05-04 Thread Russell E. Owen
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.

-- Russell

-- 
http://mail.python.org/mailman/listinfo/python-list