Good morning everyone. I am creating Mixin classes in a project to simplify the overall logic of a class diagram, for which I am using multiherences. However, the issue of multi-inheritance preferences is not very clear to me, or at least it is not clear to me how to avoid what is happening to me. Here is the simplified example:
class A:
def foo(self):
return "AAA"
class B(A):
def foo(self):
return super().foo() + "BBB"
class C(A):
def foo(self):
return "CCC"
def bar(self):
return "Other logic"
class D(B, C):
pass
print(D().foo())
In this code we have 4 classes: A
, B
, C
, D
. We can see that both B
and C
inherit from A
, but in the case of B
we have to change the logic of the method foo
, and in the case of the class C
also and adds the method bar
. What I intend with class D
and the preferences set at the time of inheriting is that it has the bar
method (that’s why it inherits from C
), but the foo
method returns "AAABBB"
which is the logic of the B
method. I am aware that what is happening is that the super()
of D
is also C
and that is why it currently returns "CCCBBB"
and that would be solved by setting:
class B(A):
def foo(self):
return A.foo() + "BBB"
However, this seems ugly to me since the whole point of relegating logic to super()
and not specifying it is to simplify changes, and in reality B
should not take into account that there may be a class D
with that expected output.
What would be the correct way for that code to output "AAABBB"
when calling the D
class foo
?