Caculate age

Alex Martelli aleax at aleax.it
Tue Feb 4 04:51:45 EST 2003


Bengt Richter wrote:
   ...
> """
> Numeric types used for keys obey the normal rules for numeric comparison:
> if two numbers compare equal (e.g., 1 and 1.0) then they can be used
> interchangeably to index the same dictionary entry.
> """
> 
> <speculation>
> IWT this would create a slight performance hit looking for a match when
> the hash value is not there from a previous operation (i.e., do you
> potentially have to hash an integer, convert to float and hash that, and

No, there's a constraint on hash(): two objects that compare equal, a==b, 
MUST ensure hash(a)==hash(b) if they're both usable as dict keys.

>>> hash(1)
1
>>> hash(1.0)
1
>>>

> This is compared to strings, that can't have equivalent values with
> different hashes AFAIK, unless maybe there is a unicode equivalency that

NO two objects can compare equal, be both usable as dict keys,
AND return different hashes.


Alex





More information about the Python-list mailing list