[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 22:43:24 EST 2015


On Fri, Dec 18, 2015 at 10:38 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> 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).

First, failing fast. I see this as a silent error waiting to happen.

Second, "NotImplemented" allows the other side to try its __eq__.


>> - 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?

Yes, I'm just saying that's an option.


More information about the Python-ideas mailing list