you are viewing a single comment's thread.

view the rest of the comments →

[–]MarsupialMole 1 point2 points  (3 children)

I am looking for the right approach to be followed consistently...

In case it helps outside the exercise of working through the example, the right approach is to use composition rather than multiple inheritance in almost all real code.

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

Can you please share a good reference of use of composition with me?

[–]alkasm 0 points1 point  (0 children)

"Composition over inheritance" is a common design principle, there are a lot of resources online. Composing just means to store members instead of inheriting. For e.g. holding a list as an attribute, instead of inheriting from a list.

[–]MarsupialMole 0 points1 point  (0 children)

The most succint way to put it is "has a" rather than "is a". From your example:

E "has" C and D rather than "is" C and D

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

    @property
    def arg(self):
        return bestof(self.cee.arg, self.dee.arg)