Assume I have a class A and a class B that is derived from A. I want to pickle/unpickle an instance of class B. Both A and B define the __getstate__/__setstate__ methods (Let’s assume A and B are complex, which makes the use of __getstate__ and __setstate__ necessary). How should B call the __getstate__/__setstate__ methods of A? My current, but perhaps not the ‘right’ approach:
class A(object):
def __init__():
self.value=1
def __getstate__(self):
return (self.value)
def __setstate__(self, state):
(self.value) = state
class B(A):
def __init__():
self.anothervalue=2
def __getstate__(self):
return (A.__getstate__(self), self.anothervalue)
def __setstate__(self, state):
superstate, self.anothervalue = state
A.__setstate__(self, superstate)
I would use
super(B,self)to get instances ofBto call the methods ofA:See this article for more info on method resolution order (MRO) and super.