python - Use methods of parent class A from parent class B -
i have class a:
class a(object): def pprint(x): print(x)
then have class b:
class b(object): def pprint(x): x += 1 # find way call a.pprint(x)
then have child class:
class child(b, a): pass
which should used:
child = child() child.pprint(1) >>> 2
i can make changes b not a. cannot refer directly in b. b never instantiated directly, via children class.
after explanation - need not super()
need sibling_super()
find next class in multiple inheritance chain. can poll python's mro that, example:
class a(object): def pprint(self, x): # make valid, assuming valid in real code print(x) class b(object): @staticmethod def sibling_super(cls, instance): mro = instance.__class__.mro() return mro[mro.index(cls) + 1] def pprint(self, x): x += 1 self.sibling_super(b, self).pprint(self, x) class child(b, a): pass child = child() child.pprint(1) # 2
Comments
Post a Comment