<div dir="ltr"><div class="gmail_default" style="font-family:monospace,monospace"><span style="font-family:arial,sans-serif">On Wed, Nov 15, 2017 at 11:49 PM, Neil Girdhar </span><span dir="ltr" style="font-family:arial,sans-serif"><<a href="mailto:mistersheik@gmail.com" target="_blank">mistersheik@gmail.com</a>></span><span style="font-family:arial,sans-serif"> wrote:</span><br></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Sometimes I get MRO failures when defining classes.  For example, if<div><br><div><div>R < E, C</div><div>B < S, E</div><div>S < C</div><div>Z < B, R</div><div><br></div><div>Then Z cannot be instantiated because C precedes E in B and E precedes C in R.  The problem is that when the inheritance graph was topologically-sorted in B, the order was S, C, E.  It could just as easily have been S, E, C.  So, one solution is to add C explicitly to B's base class list, but this is annoying because B might not care about C (it's an implementation detail of S).  It also means that if the hierarchy changes, a lot of these added extra base classes need to be fixed.</div></div></div><div><br></div><div>I propose adding a magic member to classes:</div><div><br></div><div>__precedes__ that is a list of classes.  So, the fix for the above problem would be to define E as follows:</div><div><br></div><div>class E:</div><div>    from whatever import C</div><div>    __precedes__ = [C]</div><div><br></div><div>Then, when topologically-sorting (so-called linearizing) the ancestor classes, Python can try to ensure that E precedes C when defining B.</div><div><br></div></div></blockquote><div><br></div><div><div class="gmail_default" style="font-family:monospace,monospace">So it sounds like you are talking about the way that the siblings in the inheritance tree (the bases of each class) get "flattened" into the mro in a depth-first manner, and the relative order of siblings is not preserved. What would you think about not topologically sorting the inheritance tree as such, but sorting a graph that has additional edges according to the base lists of each class involved? In this case those edges would be E->C, S->E, B->R. Is this what your talking about, or do I misinterpret the problem?</div><br></div><div><div class="gmail_default" style="font-family:monospace,monospace">​​-- Koos</div><br></div></div><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">+ Koos Zevenhoven + <a href="http://twitter.com/k7hoven" target="_blank">http://twitter.com/k7hoven</a> +</div>
</div></div>