Why do we need Method Resolution Order? Let's say:
We're happily doing OO programming and building a class hierarchy.
Our usual technique to implement the
do_your_stuff()method is to first call
do_your_stuff()on the base class, and then do our stuff.
Example 2.1. Usual base call technique
class A(object): def do_your_stuff(self): # do stuff with self for A return class B(A): def do_your_stuff(self): A.do_your_stuff(self) # do stuff with self for B return class C(A): def do_your_stuff(self): A.do_your_stuff(self) # do stuff with self for C return
We subclass a new class from two classes and end up having the same superclass being accessible through two paths.
Example 2.2. Base call technique fails
class D(B,C): def do_your_stuff(self): B.do_your_stuff(self) C.do_your_stuff(self) # do stuff with self for D return
Now we're stuck if we want to implement
do_your_stuff(). Using our usual technique, if we want to call both
C, we end up calling
A.do_your_stuff()twice. And we all know it might be dangerous to have
Ado its stuff twice, when it is only supposed to be done once. The other option would leave either
B's stuff or
C's stuff not done, which is not what we want either.
There are messy solutions to this problem, and clean ones. Python, obviously, implements a clean one which is explained in the next section.