On 2019-07-23 21: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?

Best regards,
Kristian Klette


[0]: https://bugs.python.org/issue37585
[1]: https://github.com/python/cpython/pull/14737

Well, the keys can function as a set, and, in fact, can equal a set:

>>> {'a': 1234}.keys() == {'a'}
True

so the keys of 2 dicts can be compared efficiently.

The items of 2 dicts can also be compared efficiently because you still have the keys, so you can check the key efficiently and then check the value.

However, when comparing the values you have a problem: you have 2 collections of objects that might contain duplicates, might not be hashable, and might not be sortable, so comparing them could be inefficient, and you can't refer back to their keys like in the case of comparing the items as above because the 2 dicts might have different keys. Unless someone can come up with an efficient solution, I'd probably go with raising an exception.
_______________________________________________
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/SBIYA5JAB3AUVBLQDXMT4CFSYSU76CNF/

Reply via email to