不要一说到 super 就想到父类!super 指的是 MRO 中的下一个类!

    1. class Root(object):
    2. def __init__(self):
    3. print("this is Root")
    4. class B(Root):
    5. def __init__(self):
    6. print("enter B")
    7. # print(self) # this will print <__main__.D object at 0x...>
    8. super(B, self).__init__()
    9. print("leave B")
    10. class C(Root):
    11. def __init__(self):
    12. print("enter C")
    13. super(C, self).__init__()
    14. print("leave C")
    15. class D(B, C):
    16. pass
    17. d = D()
    18. print(d.__class__.__mro__)
    1. enter B
    2. enter C
    3. this is Root
    4. leave C
    5. leave B
    6. (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.Root'>, <type 'object'>)

    首先,我们获取 self.class.mro,注意这里的 self 是 D 的 instance 而不是 B 的

    1. (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.Root'>, <type 'object'>)