Put default setstate and getstate on object for use in coöperative inheritance.
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: class DefaultSetstateAndGetstate: """ Define default getstate and setstate for use in coöperative inheritance. """ def __getstate__(self): return self.__dict__.copy() def __setstate__(self, state): self.__dict__.update(state) I suggest that this be added to object. Best, Neil
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
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@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@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.
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
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.
Neil Girdhar wrote:
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.
The same issue exists with *any* method that you use in a cooperative super call. You need to ensure that there is a class at the end of the MRO with a method that terminates the super call chain. It's obviously infeasible to add all such possible methods to class object. You will have to provide a *very* strong reason why __getstate__ and __setstate__ should be singled out for special treatment in this regard. -- Greg
Good point. I would like to kindly retract my suggestion and thank everyone for their input. In implementing further, I realize that the default getstate I want returns {} while the current default getstate used when getstate doesn't exist returns self.__dict__. Therefore I need a superclass anyway. Any comments on my other suggestion to modify __reduce__ so that it takes into account the __getnewargs_ex__ that was added in Python 3.4 would be much appreciated. Best, Neil On Sat, Jun 7, 2014 at 7:09 PM, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
Neil Girdhar wrote:
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.
The same issue exists with *any* method that you use in a cooperative super call. You need to ensure that there is a class at the end of the MRO with a method that terminates the super call chain.
It's obviously infeasible to add all such possible methods to class object. You will have to provide a *very* strong reason why __getstate__ and __setstate__ should be singled out for special treatment in this regard.
-- Greg
_______________________________________________ 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.
participants (3)
-
Greg Ewing -
Neil Girdhar -
Steven D'Aprano