[Python-ideas] Make __reduce__ to correspond to __getnewargs_ex__

Ethan Furman ethan at stoneleaf.us
Sun Jun 8 21:13:41 CEST 2014


On 06/08/2014 10:59 AM, Neil Girdhar wrote:
> On Sun, Jun 8, 2014 at 6:03 AM, Nick Coghlan wrote:
>>
>> Those classes shouldn't use reduce for their pickling support.
>
> Of course they should?  What should they use?

They should use the methods that make sense.  Pickling is a protocol.  It will use (in, I believe, this order):

   __getnewargs_ex__

   __getnewargs__

   __reduce_ex__

   __reduce__

I'm not sure where __getstate__ and __setstate__ fit in, and happily I don't need to unless I'm subclassing something 
that makes use of them.

Anyway, back to the story.

When you call pickle.dump, that code will look for the most advanced method available on the object you are trying to 
pickle, and use it.  (Well, the most advanced method for the protocol version you have selected, that's available on the 
object.)  So, if you selected protocol 2, then the pickle code will look for __getnewargs__, but not __getnewargs_ex__, 
as __getnewargs_ex__ isn't available until protocol 4.

This is similar to iterating:  first choice for iterating is to call an object's __iter__ method, but if there isn't one 
Python will fall back to using __getitem__ using integers from 0 until IndexError is raised.

--
~Ethan~


More information about the Python-ideas mailing list