How to use super() in this complex multiple inheritance situation?

41 Views Asked by At

I'm writing a library that provides subclasses of each of two existing base classes with extra functionality.

Rather than explain the arrangement in words, here's a diagram:

Diagram

And minimal code:

class Base0:
    pass

class Base1(Base0):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.foo = something()

class Base2(Base0):
    pass

class Mixin:
    def __init__(self, bar):
        self.bar = bar
        # More code

class Child1(Base1, Mixin):
    def __init__(self, *args, **kwargs):
        Base1.__init__(self, *args, **kwargs)
        Mixin.__init__(self, some_function_of(self.foo))

class Child2(Base2, Mixin):
    def __init__(self, *args, **kwargs):
        Base2.__init__(self, *args, **kwargs)
        Mixin.__init__(self, something_else())

The Base classes are outside my control. I wrote the Mixin and Child classes. Users of my library will subclass the Child classes, so it's very important that the inheritance be sane and correct.

What I'd like to do is use super().__init__ in the Child classes rather than explicitly invoking the Base and Mixin initializers. The reason this is nontrivial is that in Child1, the value passed to the Mixin initializer can't be determined until after the Base1 initializer has run.

What is the simplest/sanest way to set this up?

0

There are 0 best solutions below