2

I am getting error in multiple inheritance. As I am new in python so I did not getting why I am unable to do so.

class A(object):
    def say_hello(self):
        print 'A says hello'


class B(A):
    def say_hello(self):
        super(B, self).say_hello()
        print 'B says hello'

class C(A):
    def say_hello(self):
        super(C, self).say_hello()
        print 'C says hello'

class D(A, B):
    def say_hello(self):
        super(D, self).say_hello()
        print 'D says hello'

DD = D()
DD.say_hello() 

I am getting Error:- Cannot create a consistent method resolution.. Why?

4

1 Answer 1

6

D inherits A and B and both have a function say_hello. And B inherits from A.

You can't multiply inherit from a base class followed by a derived class
It's impossible to define a consistent MRO that satisfies the usual MRO constraints/guarantees. As described here

You now call super(D, self).say_hello() and python does not know which say_hello to pick.
A or B?!

This as example would work:

class D(A): #D only inherits A not A and B
    def say_hello(self):
        super(D, self).say_hello()
        print 'D says hello'


DD = D()
DD.say_hello()

#output
>>>A says hello
>>>D says hello


#There are 2 connections to As say_hello
    A-----
   / \   |
  B   C  |
  |     /
  \    / 
    D

PS: Please use "self" as name for the first parameter

Update: It would pick As say_hello if the inheritance would look something like this

class A(object):
    def say_hello(cls):
        print 'A says hello'


class B():
     def say_hello(cls):
        print 'B says hello'


class C(B):
    def say_hello(cls):
        super(C, cls).say_hello()
        print 'C says hello'


class D(A, C):
    def say_hello(self):
        super(D, self).say_hello()
        print 'D says hello'


DD = D()
DD.say_hello()

Inheritance-tree:

    A
  /
  |   B
  |   |
  |   C
  \   / 
    D

Python now picks the say_hello with the least specialization e.g. A.

Sign up to request clarification or add additional context in comments.

7 Comments

Wouldn't it pick A because it is the earlier base with the method?
@JLPeyret i am sorry, I do not understand your question. Of course he will call As say_hello
But you did not answer his question you just removed multiple inheritance. My question was in regards to your 'python does not know' sentence. I think it would pick A as per OPs question, except I suspect B calling A itself might confuse it.
I think same.. JL Peyret
Have you read through the link I provided? There is a very detailed explanation on how inheritance-trees are build and when this fails.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.