super使用简介

def super(cls, inst):
    mro = inst.__class__.mro()
    return mro[mro.index(cls) + 1]
1.inst 代表MRO列表
2.定位当前类在MRO中的索引,返回索引+1的位置

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

class B(A):
    def hahaha(self):
      super().hahaha()
      #super(B,self).hahaha()
      #A.hahaha(self)
    print('B')

a = A()
b = B()
b.hahaha()  # 执行结果A,B
super(B,b).hahaha()  # 执行结果 A

 

posted @ 2020-02-10 12:05  10132714  阅读(94)  评论(0编辑  收藏  举报