我正在阅读 Effective Python 书,我找到了以下代码
class MyBaseClass(object):
def __init__(self, value):
self.value = value
print "I changed it to MyBaseClass",self.value
class TimesFiveCorrect(MyBaseClass):
def __init__(self, value):
super(TimesFiveCorrect, self).__init__(value)
self.value *= 5
print "I changed it to TimesFiveCorrect ",self.value
class PlusTwoCorrect(MyBaseClass):
def __init__(self, value):
super(PlusTwoCorrect, self).__init__(value)
self.value += 2
print "I changed it to PlusTwoCorrect ",self.value
class Factor(TimesFiveCorrect, PlusTwoCorrect):
def __init__(self, value):
super(Factor, self).__init__(value)
print "I changed it to Factor ",self.value
foo = Factor(5)
from pprint import pprint
pprint(Factor.mro())
我期望 foo 的值是27
( 5 * 5 + 2
)。但事实证明是35
。输出如下
I changed it to MyBaseClass 5
I changed it to PlusTwoCorrect 7
I changed it to TimesFiveCorrect 35
I changed it to Factor 35
[<class '__main__.Factor'>,
<class '__main__.TimesFiveCorrect'>,
<class '__main__.PlusTwoCorrect'>,
<class '__main__.MyBaseClass'>,
<type 'object'>]
我可以理解MRO,但我没有得到执行命令……不应该TimesFiveCorrect
先调用吗?