What does mro() do?
Example from django.utils.functional:
for t in type(res).mro(): # <----- this
if t in self.__dispatch:
return self.__dispatch[t][funcname](res, *args, **kw)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Follow along…:
As long as we have single inheritance,
__mro__is just the tuple of: the class, its base, its base’s base, and so on up toobject(only works for new-style classes of course).Now, with multiple inheritance…:
…you also get the assurance that, in
__mro__, no class is duplicated, and no class comes after its ancestors, save that classes that first enter at the same level of multiple inheritance (like B and C in this example) are in the__mro__left to right.Every attribute you get on a class’s instance, not just methods, is conceptually looked up along the
__mro__, so, if more than one class among the ancestors defines that name, this tells you where the attribute will be found — in the first class in the__mro__that defines that name.