首页 > 其他 > 详细

super使用简介

时间:2020-02-10 12:21:15      阅读:61      评论:0      收藏:0      [点我收藏+]
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

 

super使用简介

原文:https://www.cnblogs.com/JackShi/p/12290412.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!