[Zope-dev] Re: New-style ExtensionClasses (Zope 2.8) -- MRO issue
Tim Peters
tim at zope.com
Tue Nov 25 18:23:58 EST 2003
[Jeremy Hylton]
>> ...
>> One of the key properties of C3 is local order is honored. If
>> a class A has two base classes B and C, then a method defined in B
>> (perhaps via their base classes) will be found in B first.
>>
>> This matches the way I think about inheritance, but not the way
>> the old algorithm works.
[Dieter Maurer]
> Are you sure?
>
> The old algorithm is described in the Python Language Reference:
>
> Class attribute references are translated to lookups in this
> dictionary, e.g., "C.x" is translated to "C.__dict__["x"]". When
> the attribute name is not found there, the attribute search
> continues in the base classes. The search is depth-first,
> left-to-right in the order of occurrence in the base class list.
>
> Thus, if class "B" comes before class "C" in the list of
> base classes, "B"s attributes are found in preference of "A"s
> attributes.
There are three MRO algorithms in the mix, and it's not always clear which
are being discussed:
1. "Classic", depth-first left-to-right, used for old-style classes.
2. The new-style class MRO used in Python 2.2.
3. The distinct new-style class MRO, called C3, used in Python 2.3.
There are two properties of MROs people (well, the people who argued about
it, not necessarily people on this mailing list) agreed were desirable:
1. Local ordering is respected (what Jeremy talked about).
2. Monotonicity is preserved.
#2 is subtler, and so violations also cause subtler bugs. From:
http://www.python.org/2.3/mro.html
A MRO is monotonic when the following is true: if C1 precedes
C2 in the linearization of C, then C1 precedes C2 in the
linearization of any subclass of C. Otherwise, the innocuous
operation of deriving a new class could change the resolution
order of methods, potentially introducing very subtle bugs.
WRT these criteria:
+ Classic MRO respects local ordering, but routinely violates
monotonicity in inheritance graphs with diamonds. This is a
bigger deal for new-style classes than old because diamonds
are very common (every new-style class ultimately derives from
class "object").
+ The 2.2 new-style MRO blew it on both, although it's apparently
hard to concoct an example where monotonicity fails (the link
above gives an example requiring 9 classes). In all the simpler
examples, it preserved monotonicity where classic MRO violated
it, which is what made it attractive.
+ C3 respects both.
More information about the Zope-Dev
mailing list