Cooperative multiple inheritance issue

后端 未结 2 817

This is an extension to this question and raises a problem, which you, my fellow StackOverflowers, will hopefully be able to help me with. From the referenced question, cons

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 11:11

    My answer might be a little off. I've been looking for code to solve my particular problem. But after searching for hours I could not find good example. So I wrote this little test code. I think it is definitely cooperative multiple inheritance example. I really think someone might find it useful. So here we go!

    Basically, I had a pretty big class that I wanted to split even further but due to my particular case it had to be the same class. Also all of my child classes had their own inits that I wanted to executed after base init so to speak.

    Test Code

    """
    Testing MRO Functionality of python 2.6 (2.7)
    """
    
    
    class Base(object):
        def __init__(self, base_arg, **kwargs):
            print "Base Init with arg: ", str(base_arg)
            super(Base, self).__init__()
    
        def base_method1(self):
            print "Base Method 1"
    
        def base_method2(self):
            print "Base Method 2"
    
    
    class ChildA(Base):
        def __init__(self, child_a_arg, **kwargs):
            super(ChildA, self).__init__(**kwargs)
            print "Child A init with arg: ", str(child_a_arg)
    
        def base_method1(self):
            print "Base Method 1 overwritten by Child A"
    
    
    class ChildB(Base):
        def __init__(self, child_b_arg, **kwargs):
            super(ChildB, self).__init__(**kwargs)
            print "Child B init with arg: ", str(child_b_arg)
    
        def base_method2(self):
            print "Base Method 2 overwritten by Child B"
    
    
    class ChildC(Base):
        def __init__(self, child_c_arg, **kwargs):
            super(ChildC, self).__init__(**kwargs)
            print "Child C init with arg: ", str(child_c_arg)
    
        def base_method2(self):
            print "Base Method 2 overwritten by Child C"
    
    
    class Composite(ChildA, ChildB, ChildC):
        def __init__(self):
            super(Composite, self).__init__(base_arg=1, child_a_arg=2, child_b_arg=3, child_c_arg=4)
            print "Congrats! Init is complete!"
    
    
    if __name__ == '__main__':
        print "MRO: ", str(Composite.__mro__), "\n"
        print "*** Init Test ***"
        test = Composite()
        print "*** Base Method 1 Test ***"
        test.base_method1()
        print "*** Base Method 2 Test ***"
        test.base_method2()
    

    Output

    MRO:  (, 
           , 
           , 
           , 
           , 
           )
    
    *** Init Test ***
    Base Init with arg:  1
    Child C init with arg:  4
    Child B init with arg:  3
    Child A init with arg:  2
    Congrats! Init is complete!
    *** Base Method 1 Test ***
    Base Method 1 overwritten by Child A
    *** Base Method 2 Test ***
    Base Method 2 overwritten by Child B
    

提交回复
热议问题