I have code like this:
class X(object):
def __init__(self):
print('X')
def Print(self):
print('X')
class Y(object):
def __init__(self):
print('Y')
def Print(self):
print('Y')
class Z(X,Y):
def __init__(self):
print('Z')
def Print(self):
print('z')
super().Print()
>>> z=Z()
Z
>>> z.Print()
X
It searches for Print according to
Z.__mro__
(<class '__main__.Z'>, <class '__main__.X'>, <class '__main__.Y'>, <class 'object'>)
and find it for first time in X.
But if I want to z.Print() run Y.Print(), I can use an explicit class name like:
class Z(X,Y):
def __init__(self):
print('Z')
def Print(self):
print('z')
Y.Print()
but this is not dynamic. Is there a better way to do this?
class Z(Y, X)if you want the classes to be inherited in the other order. Is that what you're asking?self.__class__.__bases__[1].Print()if you always want to pick the 2nd parent class, but if you do, remind me never to work with you..