Reputation: 3382
class Foo1:
def hi(self, name):
print('Oh hi there %s.' % name)
class Foo2:
def hi(self, name):
print('Hi %s, how ya doing?' % name)
class Bar(Foo1, Foo2):
def hi(self, name):
super(Bar, self).hi(name)
bar = Bar()
bar.hi('John')
Outputs:
Oh hi there John.
How do you access Foo2's super method instead from Bar, other than just swapping the order of "Foo1, Foo2"?
Upvotes: 0
Views: 47
Reputation: 155428
If you want to bypass the normal method resolution order, you're stuck with hacks. Either:
Pretend to be the class just before the one you really want in the MRO:
def hi(self, name):
super(Foo1, self).hi(name) # I'm really Bar's method, but lying to say I'm Foo1's
Explicitly invoke the class you care about (manually passing self
):
def hi(self, name):
Foo2.hi(self, name) # Looking it up directly on the class I want
A note: If you're using Python 3 and want the normal MRO, you don't need to pass arguments to super()
at all, this will invoke Foo1.hi
just fine:
def hi(self, name):
super().hi(name)
Upvotes: 3