Method resolution for super(Class, obj).
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Sun Sep 10 17:49:06 EDT 2006
ddtl a écrit :
> On 7 Sep 2006 10:42:54 -0700, in comp.lang.python you wrote:
>
>
>>Let's examine what the mro order is for class D:
>>
>>>>>D.mro()
>>
>>[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>,
>><class '__mai
>>n__.A'>, <type 'object'>]
>>
>>When you call d.met(), the call dispatches to the D.met() method.
>>After printing out 'D.met', you use super() to get the next class in
>>the mro order, and call that class's met method.
>>
>>As shown with the mro(), the class after D is B. So B.met() is called.
>>Normally, we would be done. But take a look at B's method!
>>
>>
>>>class B(A):
>>> def met(self):
>>> print 'B.met'
>>> super(B,self).met()
>>
>>B.met calls super, and invokes the next met method! So, the code does
>>exactly what you've asked it to do, and searches for the next class
>>after B in the mro list: class C.
>
>
> But when super(B,self).met() is invoked, isn't it supposed to look
> at MRO order of *B*,
No. It's supposed to look at the MRO of self for what comes after B.
More information about the Python-list
mailing list