hashing an array - howto

John Machin sjmachin at lexicon.net
Sat Sep 6 01:55:27 CEST 2008


On Sep 6, 9:30 am, John Machin <sjmac... at lexicon.net> wrote:
> On Sep 6, 7:49 am, bearophileH... at lycos.com wrote:
>
> > John Machin:
>
> > > Consider this:>>> hash(123) == hash(123.0) == hash(123L)
> > > True
>
> > Right... Can you explain me why Python designers have chosen to build
> > a hash() like that?
>
> I can't channel them; my rationalisation is this:
>
> Following the Law of Least Astonishment,>> 123 == 123.0 == 123L
>
> True
>
> Consequently if x == y, then adict[x] and adict[y] should give the
> same result.
>

Another reason for not folding in the type of the object is this:

>>> type([])
<type 'list'>
>>> hash(type([]))
505252536
>>> id(type([]))
505252536

IOW hash(T) == id(T) where T is a type. id(obj) is just a memory
address which can vary between executions of the same Python binary on
the same machine ... not very reproducible. There is no guarantee in
the docs for hash about under what circumstances hash(x) != hash(x) of
course; I'm just relying on the least astonishment law again :-)

And, again, we don't know what the OP's full requirements are ...




More information about the Python-list mailing list