understanding the mro (long)

Rolando Espinoza La Fuente darkrho at gmail.com
Sat Jul 24 00:43:19 EDT 2010


On Sat, Jul 24, 2010 at 12:28 AM, Benjamin Kaplan
<benjamin.kaplan at case.edu> wrote:
[...]
>
> And second, not to in any way diminish the work you did tracing out
> the inheritance tree and working through the inheritance, but Python
> has easier ways of doing it :)
>
>>>> BBar.__mro__
> (<class '__main__.BBar'>, <class '__main__.B'>, <type
> 'exceptions.RuntimeError'>, <type 'exceptions.StandardError'>, <class
> '__main__.Bar'>, <type 'exceptions.Exception'>, <type
> 'exceptions.BaseException'>, <type 'object'>)

Yes, actually I looked at __mro__ to confirm that I was right.

>>>> '__str__' in BBar.__dict__
> False
>>>> '__str__' in Bar.__dict__
> True

I see! I couldn't figure out how to find if a method is defined within
given class.

>>>> for cls in BBar.__mro__ :
>        if '__str__' in cls.__dict__ :
>                print cls
>                break
>
>
> <class '__main__.Bar'>

This is good one! It could save time figuring out where a method comes from.
Anyway, was a good exercise to figure out the mro by hand :)

Thanks for your comments Benjamin and Steven.

~Rolando



More information about the Python-list mailing list