pickle question: sequencing of operations

Ian Kelly ian.g.kelly at gmail.com
Wed May 9 16:40:00 EDT 2012


On Wed, May 9, 2012 at 2:34 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Wed, May 9, 2012 at 1:39 PM, Russell E. Owen <rowen at 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



More information about the Python-list mailing list