* DOC: OrderDict.values() comparisons in Python 3 * Src: https://hg.python.org/cpython/file/f2a0a4a45292/Doc/library/collections.rst#...
What should it say?
```rst
.. `< https://hg.python.org/cpython/file/f2a0a4a45292/Doc/library/collections.rst#...
`__
collections.OrderedDict.values().__eq__
* "is suprising" * Python 3 has `dict views`_
* :class:`OrderedDict` matches the dict interface in Python 2.7 and Python 3. * https://docs.python.org/2/library/collections.html#collections.OrderedDict * https://docs.python.org/3/library/collections.html#collections.OrderedDict
* Python 2 dict interface: * dict.viewkeys(), dict.viewvalues(), dict.viewitems() * dict.keys(), dict.values(), dict.items() * https://docs.python.org/2/library/stdtypes.html#dict * https://docs.python.org/2/library/stdtypes.html#dict.values * https://docs.python.org/2/library/stdtypes.html#dictionary-view-objects * Python 3 dict interface (:ref:`dictionary view objects`: * dict.keys(), dict.values(), dict.items() * list(dict.keys()), list(dict.values()), list(dict.items()) * https://docs.python.org/3/library/stdtypes.html#dict * https://docs.python.org/3/library/stdtypes.html#dict.values * https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects
* In order to compare OrderedDict.values() **by value** you must either:
* Cast values() to a sequence (e.g. a list) before comparison * Subclass :class:`OrderedDict` and wrap `values()`
.. code:: python
from collections import OrderedDict a = 'a' x = 1 y = x ab = [( a, x), ('b', 2)] ba = [('b', 2), (a, y)] ab_odict = OrderedDict(ab) ab_odict_ = OrderedDict(ab) ab_odict_copy = OrderedDict(ab.copy()) ba_odict = OrderedDict(ba) ab_dict = dict(ab) ba_dict = dict(ba)
# In Python 3, # OrderedDict.values.__eq__ does not compare by value: assert ( ab_odict.values() == ab_odict_.values()) is False assert (list(ab_odict.values()) == list(ab_odict_.values()) is True
# In Python 2.7 and 3, # OrderedDict.__eq__ compares ordered sequences assert (ab_odict == ab_odict_) is True assert (ab_odict == ab_odict_copy) is True assert (ab_odict == ba_odict) is False assert (ab_dict == ba_dict) is True
# - [ ] How to explain the x, y part? # - in terms of references, __eq__, id(obj), __hash__
```
On Wed, Jan 20, 2016 at 12:39 PM, Sven R. Kunze srkunze@mail.de wrote:
Documentation is a very good idea.
Maybe, even raise an error when comparing values.
Best, Sven
On 20.01.2016 12:13, Alexandre Figura wrote:
If we put technical considerations aside, maybe we should just ask to ourselves what behavior do we expect when doing equality tests between ordered dictionaries. As a reminder:
xy = OrderedDict([('x', None), ('y', None)]) yx = OrderedDict([('y', None), ('x', None)]) xy == yx
False
xy.items() == yx.items()
True
xy.keys() == yx.keys()
True
xy.values() == yx.values()
False
So, it appears that:
- equality tests between odict_values use objects identity and not
equality, 2. equality tests between odict_keys do not respect order.
If it is not technically possible to change the current implementation, maybe all we can do is just add a warning about current behavior in the documentation?
On Mon, Jan 11, 2016 at 4:17 AM, Guido van Rossum < guido@python.org guido@python.org> wrote:
Seems like we dropped the ball... Is there any action item here?
-- --Guido van Rossum (python.org/~guido http://python.org/%7Eguido)
Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Python-ideas mailing listPython-ideas@python.orghttps://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/