[Python-ideas] Put default setstate and getstate on object for use in coöperative inheritance.

Neil Girdhar mistersheik at gmail.com
Sat Jun 7 08:10:15 CEST 2014


Hi Steven,

If you don't know about getstate and setstate, I suggest you take a look at
the documentation:
https://docs.python.org/3.3/library/pickle.html#object.__getstate__.
 Besides allowing objects to be pickled, providing these methods allows
them to be copied with the copy module.

Some of the pickling and copying support can be provided by getnewargs, but
this was unfortunately almost useless for cooperative inheritance.
Luckily,  getnewargs_ex was recently added, which fills in this hole (each
subclass fills in the keyword arguments it wants to pass to __new__ and
calls super for the rest).

Best,

Neil


On Sat, Jun 7, 2014 at 1:14 AM, Steven D'Aprano <steve at pearwood.info> wrote:

> On Fri, Jun 06, 2014 at 06:53:30PM -0700, Neil Girdhar wrote:
> > When implementing getstate in coöoerative inheritance, the typical thing
> to
> > do is to call super to get dictionary and add the appropriate entries.
> > Setstate is similar: you extract what you need out of the dictionary and
> > call super with the remaining entries.  Unfortunately, object does not
> have
> > a default implementation, so you need a base class like so:
>
> I'm afraid you're going to need to explain in more detail what you're
> talking about. Even a link to a discussion elsewhere. I've used
> cooperative inheritance without needing to write a getstate or setstate
> method, so I have no idea why you think these are important enough to go
> into the base object. I presume you're not talking about serialization
> formats? That's where I would normally expect to find a getstate and
> setstate.
>
> It might also help if you can do a survey of other languages, like Java
> and Ruby, and tell us if they have such methods in the base object.
>
>
> --
> Steven
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at 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/QkvOwa1-pHQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140607/7480f3a6/attachment.html>


More information about the Python-ideas mailing list