tuple.__hash__()
Thomas Guettler
guettli at thomas-guettler.de
Tue Jan 6 09:18:22 EST 2004
Am Tue, 06 Jan 2004 04:04:01 -0500 schrieb Mike C. Fletcher:
> Just wrote:
> AFAIK, the dictionary class simply uses the hash to sort into buckets,
> providing fast indexing, before using equality to check whether the two
> keys are equal.
Thank you, that what I was asking for.
Here is a sample script which checks this:
from types import *
class MyHash:
def __init__(self, id):
assert(type(id)==IntType)
self.id=id
def __hash__(self):
# All objects have the same hash value
return 1
def __cmp__(self, b):
return cmp(self.id, b.id)
def __repr__(self):
return "<MyHash id=%s>" % self.id
def main():
d={}
i1=MyHash(1)
d[i1]=1
i2=MyHash(2)
d[i2]=1
i3=MyHash(3)
d[i3]=1
print d.items()
# id=1 twice
d[MyHash(1)]=1
print d.items()
# All right there are only three elements in the dict
# Dictionaries use __cmp__ if __hash__ is equal.
if __name__=="__main__":
main()
More information about the Python-list
mailing list