super的继承调用顺序
class MyBaseClass:
def __init__(self, value):
print("MyBaseClass")
self.value = value
class TimesFive(MyBaseClass):
def __init__(self, value):
super(TimesFive, self).__init__(value)
print("TimesFive")
self.value *= 5
class PlusTwo(MyBaseClass):
def __init__(self, value):
super(PlusTwo, self).__init__(value)
print("PlusTwo")
self.value += 2
class GoodWay(TimesFive, PlusTwo):
def __init__(self, value):
print("GoodWay")
super(GoodWay, self).__init__(value)
foo = GoodWay(5)
print('Should be 5*(5+2) and is ', foo.value)
(调用顺序和内部继承的顺序相关)
结果为:
GoodWay
MyBaseClass
TimesFive
PlusTwo
Should be 5*(5+2) and is 27
调用顺序为:
GoodWay
PlusTwo
TimesFive
MyBaseClass