Search code examples
pythoninheritancetypeerrormethod-resolution-order

TypeError: Cannot create a consistent method resolution order (MRO)


This is the code which I plan to use for my game, but it complains about an MRO error:

class Player:
    pass

class Enemy(Player):
    pass

class GameObject(Player, Enemy):
    pass

g = GameObject()

Solution

  • Your GameObject is inheriting from Player and Enemy. Because Enemy already inherits from Player Python now cannot determine what class to look methods up on first; either Player, or on Enemy, which would override things defined in Player.

    You don't need to name all base classes of Enemy here; just inherit from that one class:

    class GameObject(Enemy):
        pass
    

    Enemy already includes Player, you don't need to include it again.