1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| class A: def method(self): print("CommonA")
class B(A): pass
class C(A): def method(self): print("CommonC")
class D(B, C): pass
print("test : {}".format(D().method())) # python2输出 CommonA test : None
# python3输出 CommonC test : None
import inspect inspect.getmro(D) # python2需要inspect模块 (<class __main__.D at 0x101ebab48>, <class __main__.B at 0x101ebaa78>, <class __main__.A at 0x101ebaa10>, <class __main__.C at 0x101ebaae0>)
print(D.__mro__) # python3输出 (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
|