jeudi 5 octobre 2017

In python multiple inheritance, is it possible for one parent to access another parent without knowing what that parent is?

In my code, I have the inheritance relation B -> C -> D, and there is a particular function f of interest here

class C(B):
    def f(self):
        if <some condition>:
            B.f(self)
        else:
            <do other stuff>

And the issue is that there are multiple possibilities of B in reality, but the logic of C is the same. A straightforward way is then to use linear inheritance multiple times, i.e.,

B1 -> C1 -> D1
B2 -> C2 -> D2
B3 -> C3 -> D3

However, it is wasteful. Then I am thinking of using multiple inheritance

D1(C, B1)
D2(C, B2)
D3(C, B3)

Then the problem is how does C.f() access B1.f() (same for B2, B3) since it only knows the existence of B1 via D1?

Is this a bad idea to use multiple inheritance in this case? Or is there a better way than both the straightforward linear inheritance with multiple Cs and multiple inheritance (if it works)?

Aucun commentaire:

Enregistrer un commentaire