git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Understanding the MRO with multiple inheritance


Thanks Chris and Dieter. I think I got it. It seems it follows the __mro__ of the caller class, not the current class __mro_. 

if __name__ == '__main__':
    print('MRO of SortedIntList {}'.format(SortedIntList.__mro__))
    print('MRO of IntList {}'.format(IntList.__mro__))

# MRO of SortedIntList (<class '__main__.SortedIntList'>, <class '__main__.IntList'>, <class '__main__.SortedList'>, <class '__main__.SimpleList'>, <class 'object'>)
# MRO of IntList (<class '__main__.IntList'>, <class '__main__.SimpleList'>, <class 'object?>)

I thought obj.add(0) goes to the IntList by following its factory class __mro__, and then it follows the __mro__ of the current class(IntList) which is SimpleList .

Thanks again.


Thanks,

Arup Rakshit
ar at zeit.io



> On 30-Mar-2019, at 7:02 AM, Chris Angelico <rosuav at gmail.com> wrote:
> 
> On Fri, Mar 29, 2019 at 11:54 PM Arup Rakshit <ar at zeit.io> wrote:
>> 
>> Now when I call the add method on the SortedIntList class?s instance, I was expecting super.add() call inside the IntList class add method will dispatch it to the base class SimpleList. But in reality it doesn?t, it rather forwards it to the SortedList add method. How MRO guides here can anyone explain please?
>> 
> 
> When you call super, you're saying "go to the next in the MRO". You
> can examine the MRO by looking at SortedIntList.__mro__ - that should
> show you the exact order that methods will be called.
> 
> ChrisA
> -- 
> https://mail.python.org/mailman/listinfo/python-list