[Python-ideas] Fwd: Why do equality tests between OrderedDict keys/values views behave not as expected?
Steven D'Aprano
steve at pearwood.info
Fri Dec 18 22:38:36 EST 2015
On Fri, Dec 18, 2015 at 05:59:39PM -0500, Franklin? Lee wrote:
> I see two options:
> - comparison is explicitly NotImplemented. Any code that used it
> should've used `is`.
We're still talking about equality between Mapping.values() views,
correct?
I strongly dislike that option. I don't know of any std lib or built-in
object which tries to prohibit equality comparisons. Of course your own
custom classes can do anything they like, including rather silly things:
class Silly:
def __eq__(self, other):
x = random.choice([0, 1, 2])
if x == 2:
raise ValueError
return bool(x)
but I think that people expect that equality tests should always
succeed, even if it falls back on the default object behaviour (namely
identity comparison).
> - comparison respects keys.
I'm not sure I understand what this means. If we have two dicts:
a = {1: None, 2: None}
b = {1: None, 3: None}
are you suggesting that `a.values() == b.values()` should return False
because the KEYS {1, 2} and {1, 3} are different?
"ValuesViews implement equality by comparing identity, because
everything else is too hard" might not be useful, but at least it makes
sense as an explanation. Whereas "ValuesViews implement equality by
comparing keys" sounds like something I might have read in "PHP, A
Fractal Of Bad Design" :-)
--
Steve
More information about the Python-ideas
mailing list