Steven D'Aprano writes:
> Regarding your observation that dict views behave poorly if they
> have unhashable values, I agree, it is both odd and makes them less
> useful. Possibly at some point between the PEP and the release of
> the feature something changed, or perhaps it's just an oversight.
I'm not sure what you expect from views, though:
Python 3.8.3 (default, May 15 2020, 14:39:37)
>>> set([[1]])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> {'a' : [1]}.keys() <= {'a' : [1], 'b' : 2}.keys()
True
>>> {'a' : [1]}.values() <= {'a' : [1], 'b' : 2}.values()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<=' not supported between instances of 'dict_values' and
'dict_values'
>>> {'a' : [1]}.items() <= {'a' : [1], 'b' : 2}.items()
True
So all of the above are consistent with the behavior of sets, except
that items views do some part of comparisons themselves to deal with
non-hashables which is an extension to set behavior. And values views
don't pretend to be sets. The ValuesView ABC is not derived from Set,
presumably because dict.values returns something like a multiset.
Most set operations on key and item views seem to convert to set and
where appropriate return set (which makes sense, since returning a
view would require synthesizing a dict to be the view of!) This means
you can't do set operations (except comparisons) on items views if any
values aren't hashable.
I'm not sure what I think about this:
>>> {'a' : 1, 'b' : 2}.values() == {'b' : 2, 'a' : 1}.values()
False
That does seem less than useful. But I guess a multiset comparison
requires an auxiliary data structure that can be sorted or a
complicated, possibly O(n^2), comparison in place.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/3UBEDD3HPQ56KUI2BJRH4VVZTRZCTUXB/
Code of Conduct: http://python.org/psf/codeofconduct/