Python 如何从父类方法中调用子类方法?

Python 如何从父类方法中调用子类方法?,python,class,parent-child,super,Python,Class,Parent Child,Super,我知道这个问题可能毫无意义,但我希望这样做是有原因的。我想调用与super() 我在类B的使用\攻击()方法中有一堆代码,我不想在使用\拼写()的父方法中复制这些代码 我想调用所示行中的子方法use\u attack() 在类B的use\u attack()方法中有一堆代码,我不想在use\u spell()的父方法中复制这些代码 然后将代码分解成父类上的方法。这正是继承的目的。子类从父类继承代码,而不是相反。来自python文档:“类型的mro属性列出了getattr()和super()使用的

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

我在
类B
使用\攻击()
方法中有一堆代码,我不想在
使用\拼写()的父方法中复制这些代码

我想调用所示行中的子方法
use\u attack()

在类B的use\u attack()方法中有一堆代码,我不想在use\u spell()的父方法中复制这些代码

然后将代码分解成父类上的方法。这正是继承的目的。子类从父类继承代码,而不是相反。

来自python文档:“类型的mro属性列出了getattr()和super()使用的方法解析搜索顺序。”

这将有助于了解继承和方法解析顺序(mro)


你让我想到了一个全新的层次。我想直到现在我才明白继承。非常感谢你的帮助!令人惊叹的!当你得到这些早期的见解时,OOP真的很有趣。很高兴我能帮忙,还有更多的事要做。:)
    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 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()