On 23.07.2019 23:59, Kristian Klette wrote:

Hi!

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.

In the bug trackers[0] and the Github PR[1], I was asked
to raise the issue on the python-dev mailing list to find
a consensus on what comparing `.values()` should do.

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 are your thoughts on the issue?

Since a hash table is an unordered container and keys(), items() and values() are iterators over it, *I would expect the results of any of the comparisons to be undefined.*

While Python 3 dicts preserve order, it's _insertion_ order, so there's no guarantee that two equal dicts will iterate over the items in the same order.

*Comparing the sequences that iterators produce goes beyond the job of iterators* (i.e. requires iterating from start to finish under the hood and constructing temporary containers), so it shouldn't be a part of their functionality. The implemented comparisons were specifically intended as convenience methods that go beyond the job of an iterator AFAICS but they fell short since in the case of `values()`, the task turned out to have too much computational complexity.

So *the comparion logic, if kept at all, should be reduced to comparing things that are intrinsic to iterators themselves:* whether they point to the same object and have the same internal state. If someone needs to specifically compare the resulting sequences, they should use separate logic dedicated to comparing sequences -- with the associated quadratic complexity et al if then need to compare regardless or order. *The current logic encourages using iterators for things they aren't designed for so it's actively confusing and harmful.*

[0]: https://bugs.python.org/issue37585
[1]: https://github.com/python/cpython/pull/14737
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/R2MPDTTMJXAF54SICFSAWPPCCEWAJ7WF/

--
Regards,
Ivan

_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/QWOL2YGLSDLCTBNXC3GUSINUB2E5ODEU/

Reply via email to