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

user188276 picture user188276 · Mar 23, 2015 · Viewed 58.2k times · Source

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()

Answer

Martijn Pieters picture Martijn Pieters · Mar 23, 2015

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.