我会用
super(B,self)
获取的实例
B
调用
A
:
import cPickle
class A(object):
def __init__(self):
self.value=1
def __getstate__(self):
return self.value
def __setstate__(self, state):
self.value = state
class B(A):
def __init__(self):
super(B,self).__init__()
self.anothervalue=2
def __getstate__(self):
return (super(B,self).__getstate__(), self.anothervalue)
def __setstate__(self, state):
superstate, self.anothervalue = state
super(B,self).__setstate__(superstate)
b=B()
with open('a','w') as f:
cPickle.dump(b,f)
with open('a','r') as f:
c=cPickle.load(f)
print(b.value)
print(b.anothervalue)
看到了吗
this article
有关方法解析顺序(MRO)和超级方法的更多信息。