Hi Steven, Have you tried implementing getstate and setstate with cooperatively inherited classes? You'll need to call super().__getstate__(), which won't exist, but it really should. What I'm proposing is to move the default behaviour out of the pickle and copy internals to an object-level implementation of getstate and setstate where I think it belongs. Regarding Guido's point that object doesn't have a dict, as weird as that is, then I think a default getstate could just check for that with hasattr and if it's missing return the empty dict. Best, Neil On Sat, Jun 7, 2014 at 5:10 AM, Steven D'Aprano <steve@pearwood.info> wrote:
On Sat, Jun 07, 2014 at 02:10:15AM -0400, Neil Girdhar wrote:
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__.
I know about getstate as it regards to pickle, that's why I asked if you were talking about serialization. Unfortunately you never mentioned pickle, or copy, you talked about cooperative inheritence which is a generic concept that applies much more broadly than just copying or serializing instances.
Besides allowing objects to be pickled, providing these methods allows them to be copied with the copy module.
objects can already be copied and pickled:
py> import copy, pickle py> x = object() py> copy.copy(x) <object object at 0xb7ce9588> py> pickle.dumps(x) b'\x80\x03cbuiltins\nobject\nq\x00)\x81q\x01.'
Copying and pickling are defined by protocols, not inheritence, so there's no need for a single root method. As the documentation states, you only need to define a __getstate__ and __setstate__ method when the default protocol behaviour is not sufficient for your class, so adding these methods to object is unnecessary.
There's a historical reason for doing it this way: in Python 2, not everything inherits from object.
-- Steven _______________________________________________ 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/QkvOwa1-pHQ/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.