hashval and Numpy

Guido van Rossum guido at python.org
Thu Oct 18 17:00:28 EDT 2001


> Michael Hudson <mwh at python.net> wrote:
> 
> |>>> a = Numeric.array([1,2])
> |>>> hash(a)
> | 56
> |>>> d = {a:1} # a will be in the 56%8-th slot (in 2.2, anyway)
> |>>> a[0] = 2
> |>>> hash(a)
> | 57
> |>>> d[a] # looks in 57%8-th slot - which is empty
> | KeyError
> |>>> b = Numeric.array([1,2])
> |>>> hash(b)
> | 56
> |>>> d[b] # looks in 56%8-th slot - but what's there is not __eq__ to b!
> | KeyError
> 
> | Moral: don't use mutable objects as dictionary keys.

Hm.  The Numeric.array object should not have defined __hash__ or
tp_hash.  It's wrong if a mutable object defines a hash function, for
exactly this reason.

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



More information about the Python-list mailing list