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

Neil Girdhar mistersheik at gmail.com
Sat Jun 7 03:53:30 CEST 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140606/e124cef4/attachment.html>


More information about the Python-ideas mailing list