[issue22192] dict_values objects are hashable
Ben Roberts added the comment: Going to close this down - implementing rich comparisons for values() does feel like the correct solution... but I don't think it's possible to implement __eq__ in better than nlogn time (and possibly space). So nothing can realistically be done, yeah. -- resolution: - rejected status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22192 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22192] dict_values objects are hashable
Raymond Hettinger added the comment: Do you want to submit a patch and tests? Also take a look at the collections.abc MappingViews. -- assignee: - rhettinger nosy: +gvanrossum priority: normal - low stage: - needs patch ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22192 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22192] dict_values objects are hashable
Serhiy Storchaka added the comment: There is a question. What builtin types should be hashable? In particular, should be hashable iterators, generators? -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22192 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22192] dict_values objects are hashable
Ben Roberts added the comment: I have a patch with tests for this (running the test suite now) but I am increasingly unsure if python isn't doing the right thing already. Intuitively, it feels wrong (to me) to allow a dict_values view to be hashable since the mapping onto which it provides a view is certainly mutable. And mutable things shouldn't be hashable, everyone knows that. However the dict_values object /itself/ doesn't violate the contract of hashability - its hash (which just uses its id()) doesn't change across its lifetime, and some_values_view == foo will only be true iff foo is some_values_view whereas e.g. some_keys_view == some_other_keys_view CAN change across the lifetime of those objects - so the asymmetry here does make sense. It is taking me a while to wrap my brain around this because I often think in terms of mutable/immutable but the only thing that's important is the hashability contract, which is not currently violated by dict_values objects. I'm certainly willing to be talked out of my change of opinion here :-), my intuition hasn't fully caught up with my logic. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22192 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22192] dict_values objects are hashable
Guido van Rossum added the comment: It's clear that dict.values() shouldn't be hashable -- using the identity is plain wrong here. But shouldn't the fix be to implement rich comparisons for dict.values()? I think just a a.keys() == set(a.keys()), a.values() should be == set(a.values()). Let's move the question about whether generators and iterators should be hashable elsewhere. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22192 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22192] dict_values objects are hashable
Serhiy Storchaka added the comment: I think just a a.keys() == set(a.keys()), a.values() should be == set(a.values()). Every element of a.keys() is hashable and unique. a.values() can contain non- hashable repeated elements. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22192 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22192] dict_values objects are hashable
Guido van Rossum added the comment: Oh. Yeah. Then I think there's nothing to do. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22192 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22192] dict_values objects are hashable
New submission from Ben Roberts: In python 3.4 these result in a TypeError: hash({}.keys()) hash({}.items()) But this succeeds: hash({}.values()) The 2.7 backports of these - viewkeys, viewitems, and viewvalues respectively - behave equivalently. See more discussion on StackOverflow: http://stackoverflow.com/questions/25293912/why-are-some-dict-views-hashable The cause appears to be that key and item views implement rich comparisons, whereas values views do not. Therefore dict_view objects use the default id()-based __hash__ implementation. Possible fix: explicitly set tp_hash to PyObject_HashNotImplemented for PyDictValues_Type - and perhaps for the other two view types as well, for symmetry. -- components: Interpreter Core messages: 225287 nosy: roippi priority: normal severity: normal status: open title: dict_values objects are hashable type: behavior versions: Python 2.7, Python 3.4 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22192 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22192] dict_values objects are hashable
Changes by Antoine Pitrou pit...@free.fr: -- nosy: +rhettinger ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22192 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22192] dict_values objects are hashable
Changes by Serhiy Storchaka storch...@gmail.com: -- nosy: +serhiy.storchaka versions: +Python 3.5 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22192 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com