super()方法

super是按照mro顺序来寻找类间的继承关系即super只服务于新式类

在D类中找super的func方法,可以写为super().func() == 父类名.func(self)
也可以写为super(D, self).func() (在python2的新式类中必须这样写)

在单继承中,super()就是找父类

class A:
    def func(self):
        print('A')


class B(A):
    def func(self):
        super().func()
        print('B')

b = B()
b.func()  # 单继承中,super就是找父类


class C(A):
    def func(self):
        super().func()
        print('C')


class D(B,C):
    def func(self):
        super().func()
        print('D')


D().func()  # 多继承中,super遵循mro顺序

 

posted @ 2020-08-20 19:34  LGQ天  阅读(264)  评论(0)    收藏  举报