[Python-ideas] pickle does not work properly with cooperative multiple inheritance. Propose: "__getnewkwargs__".

Neil Girdhar mistersheik at gmail.com
Wed Sep 18 18:21:37 CEST 2013


My understanding of cooperative multiple inheritance is that a class often 
doesn't know how your parent classes want to be constructed, pickled, etc. 
and so it delegates to its parents using super.

In general, constructors can accept keyword arguments, and forward their 
unused arguments to parents effortlessly:

class A(B):
    def __init__(self, x, **kwargs):
         super().__init__(**kwargs)

will extract x and forward kwargs.

Unfortunately, the same mechanism is not easily available for pickling 
because __getnewargs__ returns only a tuple.  If there were a 
__getnewkwargs__ method, then we could have

class A:
    def _getnewkwargs__(self):
         return {**super().__getnewkwargs(), a=self.a, b=self.b}  # (new 
unpacking from PEP448)

Note how additional kwargs are added to the dict of kwargs specified by the 
parent objects.

Best,

Neil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130918/d385828a/attachment.html>


More information about the Python-ideas mailing list