[Python-3000] dict view operations

Guido van Rossum guido at python.org
Tue Sep 4 21:14:14 CEST 2007


On 9/4/07, "Martin v. Löwis" <martin at v.loewis.de> wrote:
> Guido van Rossum schrieb:
> > On 9/4/07, "Martin v. Löwis" <martin at v.loewis.de> wrote:
> >>> Oh, and another thing: the items views can contain unhashable values
> >> That, of course, could be fixed: if the key-value pairs would only
> >> hash by key (ignoring the value), they would remain hashable.
> >
> > How would that help? The key/value pairs are ordinary tuples
>
> They would have to stop being that:
>
> class Association(tuple):
>   def __hash__(self):
>     return hash(self[0])
>
> > What use case are you thinking of that this would address?
>
> It would allow to treat the items view as a proper set (which
> it still is).

Can you give some examples? I can too easily think of examples that
fail with this approach:

d = {1: 1, 2: 2}
iv = set(d.items())
(1, 1) in iv

The latter expression would be False, (while it currently is True),
since (1,1) has a different hash value than Association((1, 1)).

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list