super() and changing the signature of cooperative methods

前端 未结 2 846
囚心锁ツ
囚心锁ツ 2021-01-27 10:12

in a multiple inheritance setting such as laid out in, how can I use super() and also handle the case when the signature of the function changes between classes in

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-27 10:42

    Please see the following code, does this answer your question?

    class A():
        def __init__(self, *args, **kwargs):
            print("A")
    
    class B():
        def __init__(self, *args, **kwargs):
            print("B")
    
    class C(A):
        def __init__(self, *args, **kwargs):
            print("C","arg=", *args)
            super().__init__(self, *args, **kwargs)
    
    class D(B):
        def __init__(self, *args, **kwargs):
            print("D", "arg=", *args)
            super().__init__(self, *args, **kwargs)
    
    class E(C,D):
        def __init__(self, *args, **kwargs):
            print("E", "arg=", *args)
            super().__init__(self, *args, **kwargs)
    
    
    # now you can call the classes with a variable amount of arguments
    # which are also delegated to the parent classed through the super() calls
    a = A(5, 5)
    b = B(4, 4)
    c = C(1, 2, 4, happy=True)
    d = D(1, 3, 2)
    e = E(1, 4, 5, 5, 5, 5, value=4)
    

提交回复
热议问题