Issue 37119: Equality on dict.values() are inconsistent between 2 and 3 (original) (raw)

Issue37119

Created on 2019-06-01 04:07 by johnlinp, last changed 2022-04-11 14:59 by admin. 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 .
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
2022-04-11 14:59:16 admin set github: 81300
2019-06-02 14:11:37 johnlinp set messages: +
2019-06-01 06:08:29 serhiy.storchaka set status: open -> closedsuperseder: dict view values objects are missing tp_richcmp and tp_as_numbernosy: + serhiy.storchakamessages: + resolution: duplicatestage: resolved
2019-06-01 05:33:47 xtreak set nosy: + rhettinger, xtreakmessages: +
2019-06-01 04:07:01 johnlinp create