在Python中使用super()进行多重继承

时间:2020-08-07 05:13:44

标签: python multiple-inheritance super

我正在用Python练习多重继承。 没有Boss类,一切都会顺利进行。非常感谢您的帮助。 我已经提到过:How does Python's super() work with multiple inheritance?

反馈:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
 41 print(archer1.__str__())
 42 print('')
---> 43 boss = Boss("Boss", 50, 50, 100)
 44 print(boss.__str__())

 in __init__(self, name, power, agility, HP)
 27 class Boss(Worrior,Archer):
 28      def __init__(self, name, power, agility, HP):
---> 29      Worrior.__init__(self, name, power, HP)
 30          Archer.__init__(self, name, agility, HP)
 31      def __str__(self):

 in __init__(self, name, power, HP)
  7 class Worrior(Player):
  8     def __init__(self, name, power, HP):
----> 9     super().__init__(HP)
 10         self.name = name
 11         self.power = power

TypeError: __init__() missing 2 required positional arguments: 'agility' and 'HP'

似乎在将Worrior类中的power属性引入之后,然后停止了。

class Player:
def __init__(self,HP):
    self.HP = HP 
def sign_in(self):
    print('player sign in')
# put the class want to extend from

class Worrior(Player):
def __init__(self, name, power, HP):
    super().__init__(HP)
    self.name = name
    self.power = power
# it's the toString() method in java
# need to override the dunder(magic) method
def __str__(self):
    return "The worrior's name: " f'{self.name} \n' \
        "He has the power:" f'{self.power}'

class Archer(Player):
def __init__(self, name, agility, HP):
    super().__init__(HP)
    self.name = name
    self.agility = agility
def __str__(self):
    return "The archer's name: " f'{self.name} \n' \
        "He has the agility:" f'{self.agility}'

class Boss(Worrior,Archer):
def __init__(self, name, power, agility, HP):
     Worrior.__init__(self, name, power, HP)
     Archer.__init__(self, name, agility, HP)
def __str__(self):
    return "The boss's name: " f'{self.name} \n' \
        "With Worrior's power " f'{self.power} \n' \
        "and With Archer's agilit" f'{self.agility}'\
        "The boss' HP is: " f'{self.HP}'
boss = Boss("Boss", 50, 50, 100)
print(boss.__str__())

3 个答案:

答案 0 :(得分:1)

@Thierry Lathuille的link是正确的阅读者,但我会尝试添加一些额外的解释。初始化程序的MRO为[Boss,Worrior,Archer,Player]。这意味着(有些令人困惑)是,当Worrior调用super()时,这实际上是指Archer,而不是Player。如果将print(super().__init__)放在每次调用该方法之前,则将在崩溃之前找到以下输出:

<bound method Worrior.__init__ of <__main__.Boss object at 0x10af5f780>>
<bound method Archer.__init__ of <__main__.Boss object at 0x10af5f780>>
Traceback (most recent call last):
...

这是Python中具有多重继承的主要恕我直言,除非您有零参数初始化程序,否则我通常建议不要这样做。

如果您尝试显式调用每个基类初始化器(例如Player.__init__),那么最终您的某些初始化器将执行多次。

为了能够传递您的论点,您需要利用** kwargs。此外,随着这些参数的传递,每个唯一的参数名称都将从kwarg中删除。因此,这意味着您不能将name用作初始化参数。我将改用worrior_namearcher_name。因此,将所有这些放在一起,以下将只对每个初始化程序运行一次,而不会崩溃:

class Player:
    def __init__(self, hp, **kwargs):
        print(super().__init__)
        self.hp = hp

    def sign_in(self):
        print('player sign in')


class Worrior(Player):
    def __init__(self, worrior_name, power, **kwargs):
        super().__init__(**kwargs)
        self.name = worrior_name
        self.power = power

    def __str__(self):
        return "The worrior's name: " f'{self.name} \n' \
               "He has the power:" f'{self.power}'


class Archer(Player):
    def __init__(self, archer_name, agility, **kwargs):
        super().__init__(**kwargs)
        self.name = archer_name
        self.agility = agility

    def __str__(self):
        return "The archer's name: " f'{self.name} \n' \
               "He has the agility:" f'{self.agility}'


class Boss(Worrior, Archer):
    def __init__(self, name, power, agility, hp):
        super().__init__(archer_name=name, worrior_name=name, power=power, agility=agility, hp=hp)

    def __str__(self):
        return "The boss's name: " f'{self.name} \n' \
               "With Worrior's power " f'{self.power} \n' \
               "and With Archer's agilit" f'{self.agility}' \
               "The boss' hp is: " f'{self.hp}'

答案 1 :(得分:1)

之所以发生这种情况,是因为新样式类中的方法解析顺序为(MRO)。 老板级的MRO-

ipdb> Boss.mro()                                                                                                                   
[<class '__main__.Boss'>, <class '__main__.Worrior'>, <class '__main__.Archer'>, <class '__main__.Player'>, <class 'object'>]
ipdb>  

这意味着Worrior类中的super()调用实际上是指Archer,而不是Player。

您可以参考这篇堆栈溢出文章-Method Resolution Order (MRO) in new-style classes?,它很好地解释了MRO如何在python中工作。

答案 2 :(得分:0)

似乎Python决定在Warrior类中到达super()时再次决定遍历整个层次结构(我已经纠正了您的拼写,不是'Worrior')。

我不确定您出了什么问题,或者不确定是否只是无法使用super()的情况。我期望和您一样,并对结果感到惊讶。

但是,下面的代码没有问题,尽管您当然会失去灵活性,而且我不喜欢这样做:

class Player:
    def __init__(self, hp):
        self.hp = hp

    def sign_in(self):
        print('player sign in')


class Warrior(Player):
    def __init__(self, name, power, hp):
        Player.__init__(self, hp)
        self.name = name
        self.power = power

    def __str__(self):
        return "The warrior's name: " f'{self.name} \n' \
            "He has the power:" f'{self.power}'


class Archer(Player):
    def __init__(self, name, agility, hp):
        Player.__init__(self, hp)
        self.name = name
        self.agility = agility

    def __str__(self):
        return "The archer's name: " f'{self.name} \n' \
            "He has the agility:" f'{self.agility}'


class Boss(Warrior, Archer):
    def __init__(self, name, power, agility, hp):
        Warrior.__init__(self, name, power, hp)
        Archer.__init__(self, name, agility, hp)

    def __str__(self):
        return "The boss's name: " f'{self.name} \n' \
            "With Warrior's power: " f'{self.power} \n' \
            "and With Archer's agility: " f'{self.agility}'\
            "The boss' HP is: " f'{self.hp}'


boss = Boss("Boss", 50, 50, 100)
print(boss.__str__())

所以,这不是一个完整的答案,而是希望提供反馈-希望其他人提供一个完整的答案,并说明实际情况。