I was going through Effective Python book, And I found the folllowing code
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())
What I expected the value of foo would be 27
(5 * 5 + 2
). But it turns out to be 35
. And the output is following
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'>]
I could understand the MRO but I didn't get the execution order... Shouldn't TimesFiveCorrect
be called first?