Python高级知识点

super 函数

  • 用法
class Person:

    def say(self):
        print("Person's say method")


class Man(Person):

    def say(self):
        print("Man's say method")
        # python2 的写法
        super(Man, self).say()
        # python3 的写法
        super().say()


if __name__ == '__main__':
    man = Man()
    man.say()

  • 为什么要使用super函数来调用父类的方法
    因为在某些应用场景下,父类方法中的代码块,是需要在子类中执行的,可以节约代码,直接调用父类的方法

  • 在多重继承下 super调用父类的顺序
    super是依据MRO的继承顺序寻找父类的方法来进行调用的

  • super方法在多继承的情况下注意点
    在多继承的情况下 只要找到父类的方法执行后就不会继续寻找执行

原文地址:https://www.cnblogs.com/huameixiao/p/14732660.html