Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Multiple inheritance

Tags:

python

I have 3 classes A,B and D as given below

class A(object):
    def test(self):
        print "called A"

class B(object):
    def test(self):
        print "called B"

class D(A,B):
    def test(self):
        super(A,self).test()

inst_d=D()
inst_d.test()

----------------------------------------
Output:
  called B

Question: In D.test(), I am calling super(A,self).test(). Why is only B.test() called even though the method A.test() also exists?

like image 247
Anoop Avatar asked Jul 18 '12 18:07

Anoop


1 Answers

Because you've told it not to. In D.test you've told it to call the test method of the parent of A - that's what super does.

Normally you want to use the current class name in the super call.

like image 120
Daniel Roseman Avatar answered Oct 22 '22 02:10

Daniel Roseman