Python:__init__ 中的继承和默认值
我正在尝试创建一个具有通用 __init__
值的类,但其子类具有默认值,如下所示:
class Enemy:
def __init__(self, difficulty, power, MaxHP, magic, MaxMP, speed, name):
self.power = power + 2*difficulty
self.HP = self.MaxHP = MaxHP + 5*difficulty
self.magic = magic + 2* difficulty
self.MP = self.MaxMP = MaxMP + 5*difficulty
class Goblin(Enemy):
def __init_(self, difficulty = 1, power = 1, MaxHP = 5, magic = 1, MaxMP = 5, speed = 5, name = "Goblin"):
super(Goblin, self).__init__(self, power, MaxHP, magic, MaxMP, speed, name)
但是,当我尝试创建一个没有完整数量的默认值的 Goblin 对象时(例如,我只需输入一个难度值),它告诉我我需要完整的 8 个参数,即使其余参数都给出了默认值。有什么原因我不能这样做或者我在这里做错了什么?
I am trying to make a class with generic __init__
values, but have defaults for its subclasses, as so:
class Enemy:
def __init__(self, difficulty, power, MaxHP, magic, MaxMP, speed, name):
self.power = power + 2*difficulty
self.HP = self.MaxHP = MaxHP + 5*difficulty
self.magic = magic + 2* difficulty
self.MP = self.MaxMP = MaxMP + 5*difficulty
class Goblin(Enemy):
def __init_(self, difficulty = 1, power = 1, MaxHP = 5, magic = 1, MaxMP = 5, speed = 5, name = "Goblin"):
super(Goblin, self).__init__(self, power, MaxHP, magic, MaxMP, speed, name)
However, when I try to make a Goblin object without the full number of default values (like, I'll just put in a value for difficulty), it tells me I need the full 8 arguments even though the rest are given default values. Is there any reason I can't do that or am I doing something wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您可以毫无困难地调用
super(Goblin, self).__init__(self, power, MaxHP, magic, MaxMP, speed, name)
。如果您使用的是 2.x,您可能还想像class Enemy(object)
一样继承,以确保Enemy
是一个新样式的类(我想您必须这样做)是,考虑到您使用super
的旧方式)。这是一个更简单的示例:
输出:
编辑:
如果以下方法不起作用,则可能您的解释器中缓存了旧的类定义(尝试在新的解释器上运行它)。
Because you called
super(Goblin, self).__init__(self, power, MaxHP, magic, MaxMP, speed, name)
withoutdifficulty
. You probably also want to inherit likeclass Enemy(object)
to make sureEnemy
is a new-style class if you're on 2.x (which I guess you must be, considering the old way that you've usedsuper
).Here's a simpler example:
Outputs :
Edit:
Well if the following doesn't work, perhaps you have old class definitions cached in your interpreter (try running it on a fresh interpreter).
此代码对我有用:
我必须更改您的代码才能使其正常工作的方法:
Goblin
中拼写错误的def __init__
。Goblin()
引发TypeError: __init__() 恰好需要 8 个参数(给定 1 个)
,因为 Goblin 尚未定义__init__
方法,因此它继承了Enemy
的没有默认值的方法object
将Enemy
更改为新式类super()
时遇到TypeError: Must be type, not classobj
;我不确定旧版本的 Python 是否会允许它或触发不同的错误,但我知道旧式类与新式类具有不同的 MRO(方法解析顺序)规则,我相信这可能会使超级
无论如何都很扭曲。super(Goblin, self).__init__(self, ...)
的调用中删除第二个self
self
会自动传递给super(Class, self).some_method(...)
,因此您自己将其放在那里就像调用敌人.__init__(自我,自我,难度,力量,...)
.super(Goblin, self).__init__(...)
的调用中添加了难度
Goblin.__init__
中遇到默认困难,但随后未将值传递给Enemy.__init__
。我想就是这样。
This code works for me:
The ways I had to change yours to get it to work:
def __init__
inGoblin
.Goblin()
raisedTypeError: __init__() takes exactly 8 arguments (1 given)
, as Goblin had not defined an__init__
method, so it had inherited the one without defaults fromEnemy
Enemy
into a new-style class by inheriting fromobject
TypeError: must be type, not classobj
on the call tosuper()
; I'm not sure whether older versions of Python would have allowed it or triggered a different error, but I know old-style classes have different MRO (method resolution order) rules than new-style classes, and I believe this could makesuper
screwy anyway.self
from the call tosuper(Goblin, self).__init__(self, ...)
self
is automatically passed tosuper(Class, self).some_method(...)
, so putting it in there yourself is like callingEnemy.__init__(self, self, difficulty, power, ...)
.difficulty
to the call tosuper(Goblin, self).__init__(...)
Goblin.__init__
, but then not passing the value up toEnemy.__init__
.I think that was about it.