hash values and equality
Chris Kaynor
ckaynor at zindagigames.com
Fri May 20 12:36:00 EDT 2011
On Fri, May 20, 2011 at 9:20 AM, Chris Angelico <rosuav at gmail.com> wrote:
> On Sat, May 21, 2011 at 1:50 AM, MRAB <python at mrabarnett.plus.com> wrote:
> > [snip]
> > Is this strictly true? I thought that the hash value, an integer, is
> > moduloed (Is that how you spell it? Looks weird!) with the number of
> > array elements to give an index into the array, so different hashes
> > could give the same index, and objects with different hashes could be
> > stored in the same 'bucket'.
>
> There can always be hash collisions between different objects, but the
> assumption is that two identical objects will _always_ "collide".
I think the question was: can this dummy code ever produce a set containing
less then itemCount items (for 0 < itemCount < 2**32)?
itemCount = 100
import itertools
counter = itertools.count().next
class Test(object):
def __init__(self):
self.hash = counter()
def __hash__(self):
return self.hash
def __eq__(self, other):
return True
set((Test() for i in xrange(itemCount)))
> Chris Angelico
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110520/0f507b1d/attachment-0001.html>
More information about the Python-list
mailing list