[Python-Dev] perplexed by mro

Samuele Pedroni pedronis@bluewin.ch
Fri, 4 Oct 2002 04:39:30 +0200


From: "Greg Ewing" <greg@cosc.canterbury.ac.nz>
> Guido:
>
> > No, the whole point here is that the most inherited class's MRO
> > (i.e. z) can insert things in a base class's MRO.
>
> It worries me that when I write a super call, I'll
> never be sure which method is going to be called,
> because someone might inherit me and mess with
> my mro.
>

it can add things yes, but here is were monotonicity property comes into play,
if a mro algo is monotonic you know that however your class is subclassed the
classes in its mro will be still encountered in the same order in the mros of
the subclasses.

Sadly the naive algorithm is not monotonic.

>>> cpl.ex6(globals())
class a_boat(object): pass
class b_day_boat(a_boat): pass
class c_wheel_boat(a_boat): pass
class d_engine_less(b_day_boat): pass
class e_small_multihull(b_day_boat): pass
class f_pedal_wheel_boat(d_engine_less,c_wheel_boat): pass
class g_small_catamaran(e_small_multihull): pass
class h_pedalo(f_pedal_wheel_boat,g_small_catamaran): pass
>>> cpl.names(cpl.keeplast(cpl.lrdfs(f_pedal_wheel_boat)))
['f_pedal_wheel_boat', 'd_engine_less', 'b_day_boat', 'c_wheel_boat', 'a_boat',
'object']

the above computes the mro of f_pedal_wheel_boat according to naive algo,
b_day_boat precedes c_wheel_boat,

but for the subclass h_pedalo

>>> cpl.names(cpl.keeplast(cpl.lrdfs(h_pedalo)))
['h_pedalo', 'f_pedal_wheel_boat', 'd_engine_less', 'c_wheel_boat',
'g_small_catamaran', 'e_small_multihull', 'b_day_boat', 'a_boat', 'object']

b_day_boat follows c_wheel_boat

regards.