Newbie Python question. I have a class that inherits from several classes, and some of the specialization classes override some methods from the base class. In certain cases, I want to call the unspecialized method. Is this possible? If so, what’s the syntax?
class Base(object):
def Foo(self):
print "Base.Foo"
def Bar(self):
self.Foo() # Can I force this to call Base.Foo even if Foo has an override?
class Mixin(object):
def Foo(self):
print "Mixin.Foo"
class Composite(Mixin, Base):
pass
x = Composite()
x.Foo() # executes Mixin.Foo, perfect
x.Bar() # indirectly executes Mixin.Foo, but I want Base.Foo
you can specifically make the call you want using the syntax
in your case:
This works because Python executes calls to bound methods of the form
as if they were a call to an unbound method
so making the call in that form gives you the desired result. Inside the methods,
selfis just the instance that the method was called on, i.e, the implicit first argument (that’s explicit as a parameter)Note however that if a subclass overrides
Bar, then there’s nothing (good) that you can effectively do about it AFAIK. But that’s just the way things work in python.