I have to store objects that have two attributes (ida and idb) inside a dict. Both attributes are 64 bit positive integers and I can only store one object for a unique arrangement(combination in which the order matters) of ida and idb. For example:
obj1 = SomeClass(ida=5223372036854775807, idb=2)
obj2 = SomeClass(ida=2, idb=5223372036854775807)
obj3 = SomeClass(ida=5223372036854775807, idb=2)
Since the objects themselves are mutable, I'm using hashes of tuples '(ida, idb,)' as keys. Following the example, consider this:
d = {}
# storing obj1
k1 = hash((obj1.ida, obj1.idb,))
d[k1] = obj1
# storing obj2
k2 = hash((obj2.ida, obj2.idb,))
d[k2] = obj2
# storing obj3
k3 = hash((obj3.ida, obj3.idb,)) # note that k3 hash should be the same as k1
d[k3] = obj3
#at this point 'd' should contain only 'obj2' and 'obj3' since 'k1' == 'k3'
I tested the above example in my 64 bit machine and it worked as expected.
I have the following questions:
- Is it possible for two tuples of big integers to compute the same hash if they have differences either in elements or in ordering?
- What about in 32 bit platforms?
- If not, is there a platform independent way to guarantee that the above example will work no matter the values of ida and idb?