23.07.19 23:59, Kristian Klette пише:
During the sprints after EuroPython, I made an attempt at adding support for comparing the results from `.values()` of two dicts.
Currently the following works as expected:
``` d = {'a': 1234}
d.keys() == d.keys() d.items() == d.items() ```
but `d.values() == d.values()` does not return the expected results. It always returns `False`. The symmetry is a bit off.
Is it expected to you that `iter(d) == iter(d)` returns False? By default the equality operator returns True when and only when operands are identical. It is expected. Some objects (like numbers or strings) can override the default behavior and implement different rules for equality.
I'd argue that Python should compare the values as expected here, or if we don't want to encourage that behaviour, maybe we should consider raising an exception. Returning just `False` seems a bit misleading.
What the rule for equality do you propose? What is the use case for it? If you want to compare dict value views as ordered sequences, it can be surprised that `d1.values() != d2.values()` when `d1 == d2`. It will be inconsistent with orderless comparison of `keys()` and `items()`. If you want to compare them as unordered sequences, the computation complexity of the operation will be quadratic. Note also, that while in Python 2 always `d.values() == d.values()`, it is possible that `d1.keys() != d2.keys()` and `d1.values() != d2.values()` when `d1 == d2`. Python 3 is more consistent.