如何从父 class 方法中调用子 class 方法?

How do I call a Child class method from within a Parent class Method?

我知道这个问题可能毫无意义,但我想这样做是有原因的。我想调用与 super()

完全相反的东西
    class A(object):
        def use_attack(self, damage, passive, spells):

            #do stuff with passed parameters
            #return something

        def use_spell(self, name , enemy_hp):

            #other code      

            if name == 'Enrage':
                #call child method use_attack right here


    class B(A):
        def use_attack(self):

            #bunch of code here

            return super(B, self).use_attack(damage, passive, spells)

        def use_spell(self, name , enemy_hp):

            return super(B , self).use_attack(name ,enemy_hp)

    b = B()
    b.use_spell('Enrage', 100)

我在 class Buse_attack() 方法中有一堆代码,我不想在 use_spell() 的父方法中复制这些代码。

我想在指示的行中调用子方法 use_attack()

I have a bunch of code in class B's use_attack() method that I would not like to replicate in the parent method of use_spell() .

然后将该代码分解为 parent class 上的方法。这正是继承的目的。 Children 从 parent 继承代码,而不是相反。

来自 python 文档:"The mro attribute of the type lists the method resolution search order used by both getattr() and super()"

https://docs.python.org/3/library/functions.html#super

这应该有助于阐明继承和方法解析顺序 (mro)。

class Foo(object):
    def __init__(self):
        print('Foo init called')
    def call_child_method(self):
        self.child_method()

class Bar(Foo):
    def __init__(self):
        print('Bar init called')
        super().__init__()
    def child_method(self):
        print('Child method called')

bar = Bar()
bar.call_child_method()