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 calldo_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 returnWe subtype a new class from two classes and end up having super-supertypes being accessible through two paths.
Now we're stuck if we want to implement
do_your_stuff(). Using our usual technique, if we want to call bothBandC, we end up callingA.do_your_stuff()twice. And we all know it might be dangerous to haveAdo its stuff twice, when it is only supposed to be done once. The other option would leave eitherB's stuff orC'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.
