[Python-ideas] Fwd: Why do equality tests between OrderedDict keys/values views behave not as expected?
Franklin? Lee
leewangzhong+python at gmail.com
Fri Dec 18 23:07:16 EST 2015
On Fri, Dec 18, 2015 at 10:57 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> See my example in the other email
>> (https://mail.python.org/pipermail/python-ideas/2015-December/037498.html).
>> That's a case where the order of comparison matters, so you can't do a
>> conceptual "unordered comparison" without, in the worst case,
>> comparing everything to everything else. This is due to custom __eq__
>> (by OrderedDict, for irony's sake): a == b and b == c does not mean a
>> == c.
>
> I don't know what Guido means by "values might not be comparable", but
> your example is lack of transitivity.
>
> Mathematical equality is transitive: if a == b, and b == c, then a == c.
> But that doesn't extend to non-numeric concepts of equality, e.g.
> preference ordering, or other forms of ranking. Since Python __eq__ can
> be overridden, we cannot assume that equality of arbitrary objects is
> necessarily transitive. And indeed, even with Mappings they are not:
>
> py> from collections import OrderedDict as odict
> py> a = odict([('a', 1), ('b', 2)])
> py> b = dict(a)
> py> c = odict([('b', 2), ('a', 1)])
> py> a == b == c
> True
> py> a == c
> False
Well, that's my point AND my example.
If this were lists, then a lack of transitivity of elements would mean
a lack of transitivity for lists of those elements. You get what you
put in.
But since dict views are unordered collections, a lack of transitivity
for elements would mean INCONSISTENCY: the comparison of two views as
multisets would depend on the exact order of comparison. Unless you
are willing to compare everything to everything else in the worst
case.
More information about the Python-ideas
mailing list