Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Inherit from one class but override method calling another class?

Let say I have 3 classes: A, B and C. A is a base class for B and B is for C. Hierarchy is kept normally here, but for one method it should be different. For C class it should act like it was inherited from A.

For example like this:

class A(object):
    def m(self):
        print 'a'

class B(A):
    def m(self):
        super(B, self).m()
        print 'b'

class C(B):
    def m(self):
        super(A, self).m()
        print 'c'

So basically it should work like this:

a = A()
a.m()
a

b = B()
b.m()
a
b

c = C()
c.m()
a
c

But it is not going to work for C class, because I get this error:

AttributeError: 'super' object has no attribute 'm'

To solve this for C class I could inherit from class A, but I want to inherit everything from B and for that specific method m call super for base class A. I mean that method is one exception. Or should I call it somehow differently for class C in order to work?

How can I do that?

like image 524
Andrius Avatar asked Sep 22 '15 11:09

Andrius


1 Answers

There are in fact two ways to solve this: you can shortcut the call to super() and totally bypass the mro as in Mathias Ettinger's answer, or you can just issue the correct call to super():

class C(B):
    def m(self):
        super(B, self).m()
        print 'c'

Remember that super() expects as first argument the class from which it should start looking up the mro. It's usually the class in which the call is made, but you can pass another class upper in the mro if you want.

like image 134
bruno desthuilliers Avatar answered Oct 19 '22 23:10

bruno desthuilliers