class a(object): def __init__(self): print(‘in a‘) class b(a): def __init__(self): print(‘in b‘) super().__init__() print(‘out b‘) class c(a): def __init__(self): print(‘in c‘) super().__init__() print(‘out c‘) class d(b, c): def __init__(self): print(‘in d‘) super().__init__() print(‘out d‘) a = d() print(a.__class__.mro())
每个对象都有个属性__class__.mro()
mro是method resolve order的缩写
代码中是写的super().__init__()
super()这种写法是super(当前类名,self)的简写
然后我们直接来看看这段代码执行的结果
[Running] python "d:\pyqt5\signal.py" in d in b in c in a out c out b out d (<class ‘__main__.d‘>, <class ‘__main__.b‘>, <class ‘__main__.c‘>, <class ‘__main__.a‘>, <class ‘object‘>)
为啥中间会输出c?
c并不是b的父类啊
super其实干的是下面这件事
def super(cls, inst): mro = inst.__class__.mro() return mro[mro.index(cls) + 1]
就是在mro列表中去找下一个类,所以会产生这种输出