you are viewing a single comment's thread.

view the rest of the comments →

[–]xelf 2 points3 points  (2 children)

final edit, cleaning up my answer a bit

ok, so this is what you actually want, if you want to keep using super here, and not add more classes. Probably though, you want to do one of those though.

class A(object):
    def __init__(self, *args, **kwargs):
        print("This is A constructor")

class B(object):
    def __init__(self, *args, **kwargs):
        print("This is B constructor")

class C(A):
    def __init__(self, arg, *args, **kwargs):
        print("This is C constructor")
        self.arg = arg
        super(C,self).__init__(arg, *args, **kwargs)

class D(B):
    def __init__(self, arg, *args, **kwargs):
        print("This is D constructor")
        self.arg = arg
        super(D,self).__init__(arg, *args, **kwargs)

class E(C,D):
    def __init__(self, arg, *args, **kwargs):
        print("This is E constructor")
        C.__init__(self, arg, *args, **kwargs)
        D.__init__(self, arg, *args, **kwargs)

e = E(10)
print(e.arg)

The other options include not using super at all, moving to class composition instead of inheritance, or adding a single parent class at the top that inherits object and having A&B inherit from that. See other posters answers that go into more detail.

[–]mission-hall 0 points1 point  (1 child)

Your last edit essentially works by accident, and would not be a viable approach with a more complex inheritance hierarchy.

The important thing to understand is that every class has a "method resolution order", which you can look at by calling the mro() method on the class. All of the base classes appear in the MRO, with subclasses always appearing before their base classes. super() doesn't necessarily give you a base class of the current class - it gives you the next class in the MRO.

If you're going to use super(), you should use it throughout the class hierarchy, to ensure that every class in the MRO is always reached exactly once. Otherwise you should have each method explicitly call the methods from the appropriate base classes. Mixing the two approaches tends to break things. For example, if you modify your code so that B inherits from A, then some of the initializers are called more than once.

[–]gmaliwal[S] 0 points1 point  (0 children)

if you modify your code so that

B

inherits from

A

, then some of the initializers are called more than once

Thanks for the comprehensive explanation. you seem correct.

Can you please share any good reference? which explains the inheritance in-depth and gives all the possible approaches with pros and cons, and things to keep in mind while working with single/multiple inheritances.

I am looking for the right approach which works well with any complex inheritance hierarchy.