6

As far as I can tell, and everything I've been finding online, this should work (but it doesn't, which is why I'm asking here ;) )

class Tigon(Crossbreeds, Predator, Lion):

    def __init__(self):
        super().__init__()
    def printSize(self):
        print("Huge")

Both "Crossbreeds" and "Predator" inherit from "Mammal", and "Lion" inherits from Predator. The compilation of those works fine. I'm working on Python 3.2, though I did also try the earlier:

Edit: Sorry, part of my post didn't come through for some reason.

I also tried:

class Tigon(Crossbreeds, Predator, Lion):

    def __init__(self):
        super(Tigon, self).__init__()
    def printSize(self):
        print("Huge")

and both of them gave me:

class Tigon(Crossbreeds, Predator, Lion):
TypeError: Cannot create a consistent method resolution
order (MRO) for bases Predator, Mammal, Lion

Any suggestions?

5
  • You have to write what exactly doesn't work so we can help. Also super() normally takes arguments: docs.python.org/library/functions.html#super Commented May 19, 2011 at 10:58
  • Sorry, part of the post didn't come through. Edited now Commented May 19, 2011 at 11:02
  • @viraptor: super() in python 3 does not need arguments (though it can still take them) and it might make sense to specify them if it does not work without. Commented May 19, 2011 at 11:02
  • ah - didn't spot the python3 part, fair enough then :) Commented May 19, 2011 at 11:16
  • Check out the most comprehensive guide to super: artima.com/weblogs/viewpost.jsp?thread=236275 Commented May 19, 2011 at 15:23

3 Answers 3

7

Short answer: Don't inherit the same base class directly and indirectly, but inheriting directly after indirectly should work. So don't inherit Predator or inherit it after Lion.

Well, the C3 MRO seems to not be able to find any order consistent with all constraints. The constraints are that:

  • each class must come before it's base classes
  • and the base classes must come in the order they are listed.

You inherit Crossbreeds, Predator and Lion in that order, so their methods must be called in that order. But since Lion inherits Predator, it's methods must be called before those of Predator. Which is not possible, therefore it says it can't create consistent method resolution order.

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

1 Comment

Thanks! I get it. So that means that (Crossbreeds, Lion, Predator) should work, right? That gives me another question - using that phrasing, what should I do if I want Tigon to inherit a method that is defined differently in Lion and Predators, and I want Tigon to take it from Predators?
0

Should be super().__init__(self).

Edited: My apologize, you should put Lion front:

class Tigon(Lion, Predator, Crossbreeds):
    def __init__(self):
        super().__init__()

2 Comments

@Jan - can you tell what the problem is?
The statement is not the problem (and was correct). The inheritance is.
0

If I correctly understood the inheritance model you described, this is how you should define the Tigon class:

class Mammal(object):
    def __init__(self):
        super(Mammal, self).__init__()

class Crossbreeds(Mammal):
    def __init__(self):
        super(Crossbreeds, self).__init__()

class Predator(Mammal):
    def __init__(self):
        super(Predator, self).__init__()

class Lion(Predator):
    def __init__(self):
        super(Lion, self).__init__()

class Tigon(Lion, Crossbreeds, Predator):
    def __init__(self):
        super(Tigon, self).__init__()

t = Tigon()

this alternative is equivalent, since Lion is a Predator:

class Tigon(Lion, Crossbreeds):
    def __init__(self):
        super(Tigon, self).__init__()

Now here's a quick rule for this. Each class' constructor is called after base classes, but they must appear in the reverse order in the class definition. Also, classes that override methods defined in their parent class(es) must appear first in class definitions - this means that if you wanted to override a method of Predator in Lion, Lion should appear before Predator in the definition. The longer explanation for this is in Jan Hudec's answer.

1 Comment

See Jan's answer for a description of why

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.