Multiple inheritance supplement

Object-oriented multiple inheritance

1. __mro__ c3 algorithm and explain 
    
    ( 1) __mro__ : 
        Python class multiple inheritance feature, if the inheritance is too complicated, hard to see will first call the property or method. 
In order to easily and quickly see the inheritance and order, can __mro__ method to get the order to call this class. 
    
    ( 2 ) C3 Algorithm 
    SS A (Object): 
        Pass 
    
    class B (A):
         Pass 
    
    class C (B):
         Pass 
    
    class D (Object):
         Pass 
    
    class E (D, C):
         Pass 
    
    class F. (Object):
         Pass 
    
    class G (F.):
         Pass 
    
    class H (C, G):
         Pass 
    
    class Foo (E, H):
         Pass
    
    
    # Print (E .__ mro__) 
    # Print (H .__ mro__) 
    "" " 
    L (Foo + L (E) + L (H)) 
    
    L (E) = E, D, C, B, A, Object 
    L (H ) = H, C, B, A, G, F., Object 
    
    Foo = (Object) + (G, F., Object) 
    Foo, E, D, H, C, B, A, G, F., Object 
    "" " 
    Print (. Foo __mro__ )

 2 . classic and new classes 
    . a find first find the left and right 
        class A (Object):
             Pass 
    
        class B (Object):
             DEF F1 (Self):
                 Print ( ' B ' ) 
    
        class C (A , B):
             Pass 
    
        obj =C () 
        obj.f1 () 
    
    b Classic and new categories. 
        Py2: 
            Classic: 
            the new class: if they or their predecessors as long as someone has inherited object, it is such a new class 
        py3: 
            new class 
            
        Classic and new classes Find members sequence is not the same. 
            classic, one way to (depth first). 
            the new class, C3 algorithm (c3 python2.3 update algorithm) 
            
                Foo + (C, D, F., G) + (G , D, G, W is) + (the I, G, D, G, W is) 
                    
                FOO, 
                    obtaining a first header and other epitopes compared 
                        does not exist away 
                        , if present, to give up, and then acquires second table the header again and watch the other end of the table to compare 
                
        short-answer, 
            classic, one way to (depth-first) 
            new class, leave the root. (c3 science algorithm) 
     
        Note:
            Super __mro__ is to follow the order of execution

 

Guess you like

Origin www.cnblogs.com/kangqi452/p/11617539.html