classification
Title: Equality on dict.values() are inconsistent between 2 and 3
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: dict view values objects are missing tp_richcmp and tp_as_number
View: 12445
Assigned To: Nosy List: johnlinp, rhettinger, serhiy.storchaka, xtreak
Priority: normal Keywords:

Created on 2019-06-01 04:07 by johnlinp, last changed 2019-06-02 14:11 by johnlinp. This issue is now closed.

Messages (4)
msg344145 - (view) Author: 林自均 (johnlinp) * Date: 2019-06-01 04:07
When I create 2 different dicts with the same literal, their dict.values() are equal in python2 but not equal in python3.

Here is an example in python2:

    $ python2
    Python 2.7.16 (default, Mar  4 2019, 09:02:22)
    [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a = {'hello': 'world'}
    >>> b = {'hello': 'world'}
    >>> a.values() == b.values()
    True
    >>> a.keys() == b.keys()
    True

However, the dict.values() are not equal in python3:

    $ python3
    Python 3.7.2 (default, Feb 12 2019, 08:16:38)
    [Clang 10.0.0 (clang-1000.11.45.5)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a = {'hello': 'world'}
    >>> b = {'hello': 'world'}
    >>> a.values() == b.values()
    False
    >>> a.keys() == b.keys()
    True

Is this a bug? Or is this behavior specified somewhere in the documentation? Thanks.

Note: it's inspired by this StackOverflow question: https://stackoverflow.com/questions/56403613/questions-about-python-dictionary-equality
msg344149 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-06-01 05:33
https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects

> Keys views are set-like since their entries are unique and hashable. If all values are hashable, so that (key, value) pairs are unique and hashable, then the items view is also set-like. (Values views are not treated as set-like since the entries are generally not unique.) For set-like views, all of the operations defined for the abstract base class collections.abc.Set are available (for example, ==, <, or ^).

In Python 2 keys() and values() return a list where __eq__ is implemented. In Python 3 view objects are returned. This can be seen in Python 2 also using viewkeys and viewvalues where viewvalues() is False. So this is not a bug but I am not sure if docs can be improved to clarify this further.


$ python2
Python 2.7.14 (default, Mar 12 2018, 13:54:56)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = {'a': 1}
>>> b = {'a': 1}
>>> a.viewkeys() == b.viewkeys()
True
>>> a.viewvalues() == b.viewvalues()
False
>>> a.values() == b.values()
True
msg344150 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-06-01 06:08
See also issue12445.
msg344286 - (view) Author: 林自均 (johnlinp) * Date: 2019-06-02 14:11
Hi Karthikeyan and Serhiy,

Thank you for the explanation. I'll check the references you gave me.
History
Date User Action Args
2019-06-02 14:11:37johnlinpsetmessages: + msg344286
2019-06-01 06:08:29serhiy.storchakasetstatus: open -> closed

superseder: dict view values objects are missing tp_richcmp and tp_as_number

nosy: + serhiy.storchaka
messages: + msg344150
resolution: duplicate
stage: resolved
2019-06-01 05:33:47xtreaksetnosy: + rhettinger, xtreak
messages: + msg344149
2019-06-01 04:07:01johnlinpcreate