0

I have 2 classes each implementing a specific behavior and using parent classes.

Now I have a new class that needs to use either behavior but which one can only be determined during/after construction.

That is currently being done by using multiple inheritance. However as the base classes were not written taking this into account the contained super() call will call into the wrong sub-tree

Consider the following MWE:

class A:
    def __init__(self, use_b):
        self.init_b = use_b  # Maybe complicated

    def foo(self):
        print("A")


class B(A):
    def foo(self):
        super().foo()
        print("B")


class C1(A):
    def foo(self):
        super().foo()
        print("C1")


class C2(C1):
    def foo(self):
        super().foo()
        print("C2")


class D(B, C2):
    def __init__(self, *args):
        super().__init__(*args)
        self.use_b = self.init_b

    def foo(self):
        if self.use_b:
            B.foo(self)
        else:
            C2.foo(self)

d = D(True)
d.foo()  # Expected: "A B"

print("NEXT")
d = D(False)
d.foo()  # Expected: "A C1 C2"

Somewhere in A a property is initialized that is used in D to determine the appropriate class to use. Both of the candidates B and C2 inherit from the base class as they are/used to be independent implementations

It works for calling C2.foo but calling B.foo ends up calling foo in C2 and C1 too, which I do not want.

I do know WHY this happens (MRO) but have no good idea to resolve this.

I might be able to have D only inherit from A, initialize this and then decide which subclass to use, i.e.:

class D(A):
    def __init__(self, *args):
        super().__init__(*args)
        if self.init_b:
            self.impl = B(*args)
        else:
            self.impl = C2(*args)

    def foo(self):
        self.impl.foo()

But this has 3 issues:

  1. It initializes at least A multiple times. That is bad for performance and might fail if A.__init__ modifies the ref-type args (e.g. lists)
  2. It might fail if there are some args only B and C2 understand so A can't handle them
  3. I'll need to implement and wrap all methods of A that could be called. If I forget one that was overwritten by either of the B or C classes the behavior will be silently wrong. Similar for properties

Is there a clean and safe way to handle this scenario?

2
  • "It might fail if there are some args only B and C2 understand so A can't handle them" Does this mean that the user can already know which implementation needs to be used in advance? Commented Aug 15, 2024 at 16:29
  • No, only the code inside D will know this Commented Aug 29, 2024 at 12:24

1 Answer 1

2

You're basically asking the foo methods of B, C1 and C2 to be uncooperative, ignoring the MRO of self and instead following the MRO of the class itself, in which case you can make the uncooperative methods call super(__class__, __class__) instead to lookup the method of the parent in the class' own MRO:

class B(A):
    def foo(self):
        super(__class__, __class__).foo(self)
        print("B")

class C1(A):
    def foo(self):
        super(__class__, __class__).foo(self)
        print("C1")

class C2(C1):
    def foo(self):
        super(__class__, __class__).foo(self)
        print("C2")

Demo here

Sign up to request clarification or add additional context in comments.

1 Comment

That's actually intended for static methods, isn't it? But it does work. Am I right assuming this is the same as C1.foo(self) except it works w/o repeating the parent? Of course I now have the issue that a full inheritance must be either cooperative or not and mixing them will cause strange issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.