Python:__ init__中的继承和默认值

时间:2011-11-10 00:45:27

标签: python inheritance default 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个参数,即使其余的都给出了默认值。有什么理由我不能这样做,或者我在这里做错了什么?

2 个答案:

答案 0 :(得分:4)

因为您在没有super(Goblin, self).__init__(self, power, MaxHP, magic, MaxMP, speed, name)的情况下致电difficulty。您可能还想继承class Enemy(object),以确保Enemy是一个新式的课程,如果您使用的是2.x(我猜您必须这样,考虑到您的旧方式)使用super)。

这是一个更简单的例子:

class Animal(object):
  def __init__(self, talk):
    print '__init__ Animal: ', talk

class Cat(Animal):
  def __init__(self, talk='meow'):
    print '__init__ Cat'
    super(Cat, self).__init__(talk)

if __name__ == '__main__':
  tom = Cat()

输出:

__init__ Cat
__init__ Animal:  meow

编辑:

如果以下内容不起作用,也许您的解释器中会缓存旧的类定义(尝试在新的解释器上运行它)。

class Enemy(object):
  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
    print 'Raaarghh!! I am the formidable {}.'.format(name)

class Goblin(Enemy):
  def __init__(self, difficulty=1, power=1, MaxHP=5, magic=1, MaxMP=5, speed=5, name="Goblin"):
    super(Goblin, self).__init__(difficulty, power, MaxHP, magic, MaxMP, speed, name)

if __name__ == '__main__':
  g = Goblin(name='user1038783 goblin')

答案 1 :(得分:0)

此代码适用于我:

class Enemy(object):
    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__(difficulty, power, MaxHP, magic, MaxMP, speed, name)

我必须改变你的方式让它发挥作用:

  1. 修复def __init__中拼写错误的Goblin
    • 症状:Goblin()已提出TypeError: __init__() takes exactly 8 arguments (1 given),因为Goblin尚未定义__init__方法,因此它继承了Enemy
    • 中没有默认值的方法
  2. 通过继承Enemyobject更改为新式类
    • 症状:我TypeError: must be type, not classobj的电话号码为super();我不确定旧版本的Python是否会允许它或触发不同的错误,但我知道旧式类具有与新式类不同的MRO(方法解析顺序)规则,我相信这可能会使{{{无论如何,1}}搞砸了。
  3. super的通话中删除第二个self
    • 症状:super(Goblin, self).__init__(self, ...)会自动传递给self,因此请自行将其放入其中就像拨打super(Class, self).some_method(...)一样。
  4. Enemy.__init__(self, self, difficulty, power, ...)的来电中添加了difficulty
    • 症状:您在super(Goblin, self).__init__(...)中遇到了违约问题,但未将该值传递给Goblin.__init__
  5. 我认为那是关于它的。