How does Python pass __init__ parameters with multiple inheritance

前端 未结 3 1625
余生分开走
余生分开走 2021-01-14 12:52

I have this code, showing a classic diamond pattern:

class A:
    def __init__( self, x ):
        print( \"A:\" + x )


class B( A ):
    def __init__( self         


        
3条回答
  •  既然无缘
    2021-01-14 13:35

    You can always check the method resolution order that any class should have:

     >>> D.mro()
     [__main__.D, __main__.B, __main__.C, __main__.A, object]
    

    As you can see, if everybody is doing the right thing (i.e. calling super), the MRO will be 1st parent, 2nd parent, 1st parent's parent and so on...

    You can just think of depth first and then left to right to find the order although ever since python 2.3 the algorithm changed but the outcome is usually the same.

    In this case B and C have the same parent A and A doesn't call super

提交回复
热议问题