Reuse inheritance tree of one out of multiple classes in Python

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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()
print("NEXT")
d = D(False)
d.foo()
</code>
<code>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() print("NEXT") d = D(False) d.foo() </code>
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()

print("NEXT")
d = D(False)
d.foo()

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.:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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()
</code>
<code>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() </code>
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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật