Python-官网例子读懂MRO继承


知乎详解(官网翻译)
MRO历史

C3方法解析顺序(The C3 Method Resolution Order)

  • 像深度又像广度排序
    >>> O = object
    >>> class F(O): pass
    >>> class E(O): pass
    >>> class D(O): pass
    >>> class C(D,F): pass
    >>> class B(D,E): pass
    >>> class A(B,C): pass
    ```sh
                              6
    Level 3                 | O |                  (more general)
                          /  ---  \
                         /    |    \                      |
                        /     |     \                     |
                       /      |      \                    |
                      ---    ---    ---                   |
    Level 2        3 | D | 4| E |  | F | 5                |
                      ---    ---    ---                   |
                       \  \ _ /       |                   |
                        \    / \ _    |                   |
                         \  /      \  |                   |
                          ---      ---                    |
    Level 1            1 | B |    | C | 2                 |
                          ---      ---                    |
                            \      /                      |
                             \    /                      \ /
                               ---
    Level 0                 0 | A |              
    L[A] = A + merge(BDEO,CDFO,BC)
       = A + B + merge(DEO,CDFO,C)
       = A + B + C + merge(DEO,DFO)
       = A + B + C + D + merge(EO,FO)
       = A + B + C + D + E + merge(O,FO)
       = A + B + C + D + E + F + merge(O,O)
       = A B C D E F O
    例2
    >>> O = object
    >>> class F(O): pass
    >>> class E(O): pass
    >>> class D(O): pass
    >>> class C(D,F): pass
    >>> class B(E,D): pass
    >>> class A(B,C): pass
.
                           6
                          ---
Level 3                  | O |
                       /  ---  \
                      /    |    \
                     /     |     \
                    /      |      \
                  ---     ---    ---
Level 2        2 | E | 4 | D |  | F | 5
                  ---     ---    ---
                   \      / \     /
                    \    /   \   /
                     \  /     \ /
                      ---     ---
Level 1            1 | B |   | C | 3
                      ---     ---
                       \       /
                        \     /
                          ---
Level 0                0 | A |
                          ---
>>> A.mro()
(<class '__main__.A'>, <class '__main__.B'>, <class '__main__.E'>,
<class '__main__.C'>, <class '__main__.D'>, <class '__main__.F'>,
<type 'object'>)

什么时候对MRO来说是非法的?

-

例3
>>> O = object
>>> class X(O): pass
>>> class Y(O): pass
>>> class A(X,Y): pass
>>> class B(Y,X): pass
 -----------
|           |
|    O      |
|  /   \    |
 - X    Y  /
   |  / | /
   | /  |/
   A    B
   \   /
     ?
L[O] = 0
L[X] = X O
L[Y] = Y O
L[A] = A X Y O
L[B] = B Y X O
L[C] = C + merge(AXYO, BYXO, AB)
     = C + A + merge(XYO, BYXO, B)
     = C + A + B + merge(XYO, YXO)
  • 这时,我们不能合并列表XYO和列表YXO,:因此,没有了符合规则的head,C3算法停止。在这种情形下,Python 2.3会抛出一个异常(TypeError: MRO conflict among bases Y, X),因为X在YXO的tail中,而Y在XYO的tail中,阻止创建模棱两可的继承层次。Python 2.2不会抛出异常,而是选择了一种特定的方法解析顺序(CABXYO).

评论
  目录