On Sun, Jun 8, 2014 at 3:13 PM, Ethan Furman <ethan@stoneleaf.us> wrote:
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__

In fact, the reduce functions come first.  The order is:

reduce_ex
reduce
getnewargs_ex and getstate (3.4+)
getnewargs_ex (3.4+)
getnewargs and getstate
getnewargs
getstate
default code that copies/pickles the dict

Up until 3.3, a "default" reduce function might be:

def __reduce__(self):
     return type(self), self.__getnewargs__(), self.__getstate__()

It' s no longer possible to write a default reduce function in 3.4 given the addtion __getnewargs_ex__.  What I am suggesting is to add a new protocol so that reduce returns the keyword arguments.

Best,

Neil
 

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.

You really should find out where they fit in before you reply to a message about 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~
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

--

--- You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/zohH2BCtYzY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-ideas+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.