Understand __mro__

python-mro.md

What is mro?

method resoultion order is an algorithm to choose the right method to get executed from inherited base classes.

Where it is used?

mro will be interesting if more than one base class is used, the method you are calling is defined in more than one base class also.

please, watch every step of the below ipython-ed example

Example?

In [29]: class A(object):
    ...:     x = 10
    ...:     

In [30]: class B(A):
    ...:     pass
    ...: 

In [31]: class C(A):
    ...:     x = 15
    ...:     

In [32]: class D(B, C):
    ...:     pass
    ...: 

In [33]: d = D()

In [34]: D.__mro__
Out[34]: (__main__.D, __main__.B, __main__.C, __main__.A, object)

In [35]: d.x
Out[35]: 15   # Output of the new-style mro

In [36]: class A:
    ...:     x = 10
    ...:     

In [37]: class B(A):
    ...:     pass
    ...: 

In [38]: class C(A):
    ...:     x = 15
    ...:     

In [39]: class D(B, C):
    ...:     pass
    ...: 

In [40]: d = D()

In [41]: D.__mro__
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-41-c7e45d56683f> in <module>()
----> 1 D.__mro__

AttributeError: class D has no attribute '__mro__'

In [42]: d.x
Out[42]: 10  # output of the legacy style mro

Explanation for the above example:

class A, B, C is the usual to understand simple classes inherited from single parent class. (till ipython line #6)

After that, we started inheriting class from multiple base classes which is class D.

New style class:

If the base class is derived from object class means, it is called new-style classes.

Old-style class:

If the base class is not derived from object class, The topmost base class will be type class. It is used in legacy python classes.

the __mro__ is defined in new-style python classes. So, it helps to understand the execution order of the methods in all self and base classes.

In ipython line# 35 is output of new style class’s mro algorithm, also, the order of the execution is also printed in the next line D, B, C, A, object, similar to the breadth first approach

ipython line# 42 is the output of legacy-style class’s mro algorithm, similar to the depth-first approach, it doesn’t have __mro__ magic method to display the execution orders.

If you are an Appetite,

read the source paper of deriving this algorithm from old style to new style approach
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.19.3910&rep=rep1&type=pdf

 

Advertisements

About Navaneethan

mixed feelings...
This entry was posted in 10min, Python and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s