示例代码:
class Base(object):
def test(self):
print("----Base")
class A(Base):
def test(self):
print("-----A")
class B(Base):
def test(self):
print("-----B")
class C(A,B):
pass
#def test(self):
# print("-----C")
c = C()
c.test()
print(C.__mro__)
在上面代码中,最后一句话print(C.__mro__)
可以输出调用C类中方法时的优先顺序,结果为:(<class '__main__.C'>,<class '__main__.A'>,<class '__main__.B'>,<class '__main__.Base'>,<class 'object'>)
即优先顺序为广度优先(参见上文转载:Python 为什么要继承 object 类?(一) - 年少不知头发贵的博客 - CSDN博客 https://blog.csdn.net/qq_40877371/article/details/99335494),C>A>B>Base>object