类型错误:无法创建一致的方法解析顺序 (MRO) [英] TypeError: Cannot create a consistent method resolution order (MRO)

查看:28
本文介绍了类型错误:无法创建一致的方法解析顺序 (MRO)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我计划用于我的游戏的代码,但它抱怨 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()

推荐答案

你的 GameObject 继承自 Player and Enemy.因为Enemy已经继承自Player,Python现在无法确定首先查找哪个类;PlayerEnemy,这将覆盖 Player 中定义的内容.

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.

你不需要在这里命名Enemy的所有基类;只是从那个类继承:

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

class GameObject(Enemy):
    pass

Enemy 已经包含了 Player,不需要再次包含.

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

这篇关于类型错误:无法创建一致的方法解析顺序 (MRO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆