I can't seem to find the actual language specification on how value comparisons
have to be done by a complying implementation (e.g. CPython). Consider the simple case
a == b
The reference says that this is done using the “rich comparison” methods, in this case __eq__()
. Neither the Data Model nor the reference on Value Comparison Expressions seem to specify in detail, what else a Python implementation is required/allowed to do, especially if the type(a)
signals that it does not implement __eq__
by explicitly returning NotImplemented
.
This answer plausibly suggests that for any a == b
, CPython
- calls
b.__eq__(a)
(!) ifb
is a subclass ofa
. This makes intuitive sense, as it allows the subclass to overload all comparisons between values of itself and it's parent class (therefor also implying symmetry for both the parent and the subclass). - if the above fails, calls
a.__eq__(b)
- if the above fails, calls
b.__eq__(a)
if that hasn't happened yet; - falls back to
identity comparison
AFAICS this order makes sense, also given Python's loose sense of symmetry, reflexivity, variance and the like. My question is: Does the language guarantee that for any a == b
(or other value comparisons), a complying implementation will execute the comparison in the way described above?