As I understand it, if all classes use the "new style" then this would happen:
class A(object):
def __init__(self):
print("Entering A")
super().__init__()
print("Leaving A")
class B(object):
def __init__(self):
print("Entering B")
super().__init__()
print("Leaving B")
class C(A, B):
def __init__(self):
print("Entering C")
super().__init__()
print("Leaving C")
c = C()
Entering C
Entering A
Entering B
Leaving B
Leaving A
Leaving C
After understanding that super uses the MRO of the original object (c) it makes sense why the Entering/Leaving are the way they are. But what if I need to pass some specific arguments to A and B, but also the order in which the arguments are received in C is not similar to A and B:
class A(object):
def __init__(self, x1, x2):
print(f"Entering A [x1={x1}, x2={x2}]")
super().__init__()
print("Leaving A")
class B(object):
def __init__(self, y1, y2):
print(f"Entering B [y1={y1}, y2={y2}]")
super().__init__()
print("Leaving B")
class C(A, B):
def __init__(self, x1, y1, x2, y2):
print(f"Entering C [x1={x1}, y1={y1}, x2={x2}, y2={y2}]")
super().__init__()
print("Leaving C")
c = C('x1', 'y1', 'x2', 'y2')
I can think of just passing kwargs around all day but I feel like there might be a nicer way.
Also, what if B is part of a library so we can't change it and it uses positional arguments, not kwargs? In that case what can I do since I don't think it's a good idea to manually mess with the MRO but at the same time the constructor of B won't take the kwargs?
What is the correct way of resolving the above issue?
kwargsand get the bits you need if you need the flexibility, but "the correct" way would be to callA.__init__(self, x1, x2)andB.__init__(self, y1, y2). No magic, no ambiguity.