I'm trying to get the order of __init__ executions right. The hierarchy consists of two branches. The left branch implements some abstract methods of the right branch. In the right branch there is also a split. Now I'd like them to execute: left first top to bottom, and then right top to bottom.
Is there a way to achieve that in Python 3?
So basically the following should print the numbers in order (which it doesn't):
from abc import abstractmethod, ABCMeta
class L1:
def __init__(self):
super().__init__()
print(1)
class R1(metaclass=ABCMeta):
def __init__(self):
super().__init__()
print(3)
@abstractmethod
def m(self):
pass
class L2(L1):
def __init__(self):
super().__init__()
print(2)
def m(self):
pass
class R2a(R1):
def __init__(self):
super().__init__()
print(4)
class R2b(R1):
def __init__(self):
super().__init__()
print(4)
class X(L2, R2a):
pass
class Y(L2, R2b):
pass
x = X()
print("--")
y = Y()
Unfortunately I cannot change the order of L2 and R2 since L2 implements abstract methods of R2 (there will be errors if I change that).
Is it possible?
(Do you know a good way to think about MRO to solve such kind of problem easy?)
EDIT: I thought I doesn't matter, but I want to note that in my real structure, there is actually a diamond on the L side like L2a(L1), L2b(L1) and L2(L1a, L2a). That's to completely split some functionality.
object:class L1 (object):in order to get "new style classes".object