hashval and Numpy

Michael Hudson mwh at python.net
Fri Oct 19 10:52:58 CEST 2001


Guido van Rossum <guido at python.org> writes:

> > 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.

I was making that interactive session up to show why mutable objects
shouldn't define a hash function!

Indeed, the OP was complaining that Numeric.array *didn't* define a
hash function... (and then snipped out the bit of my post where I said
I was making it up).

circularly-ly y'rs
M.

-- 
  we're already scrubbing the face of intuition with steel wool,
  setting it on fire, then putting it out with an axe <wink>.
                      -- Tim Peters, on comparing recursive structures



More information about the Python-list mailing list