5

My understanding is that hashing two different frozensets (immutable Python sets), which need to contain hashable objects, should lead to two different hashes. Why do I get the output below for two different frozensets?

In [11]: a
Out[11]: frozenset({(2, -2), (2, -1), (3, -2), (3, -1)})

In [12]: b
Out[12]: frozenset({(4, -2), (4, -1), (5, -2), (5, -1)})

In [13]: hash(a)
Out[13]: 665780563440688

In [14]: hash(b)
Out[14]: 665780563440688
Roy
  • 3,574
  • 2
  • 29
  • 39
  • I don't know how relevant this is, but: It's not guaranteed that two unequal values will have different hash values (it couldn't be--there are a lot more than 2^64 possible hashable values), just that it's uncommon (and hopefully not predictable, because if it were you could, e.g., DoS a server by giving it a bunch of hash-colliding values and turn its dicts into linear-time instead of constant-time access). – abarnert Apr 30 '15 at 04:03
  • @abarnert I think your comment is "relevant" enough to be an answer. – Shashank Apr 30 '15 at 04:07
  • @Shashank: Well, it depends on whether he just stumbled on one example out a billion by accident, or whether something else weird is going on. Anyway, someone else already turned it into an answer. – abarnert Apr 30 '15 at 04:51

1 Answers1

5

You seem to have stumbled upon two frozensets with equal hash codes and different contents. This is not so strange as it may seem as the property of hash codes are that they are guaranteed to be equal for equal objects and probably different for non-equal objects.

From the Python docs:

hash(object) -> integer

Return a hash value for the object. Two objects with the same value have the same hash value. The reverse is not necessarily true, but likely.

The absolute easiest example of this are the numbers -1 and -2 which have the same hash code in python:

>>> print(hash(-1))
-2
>>> print(hash(-2))
-2
Raniz
  • 10,882
  • 1
  • 32
  • 64
  • 1
    It's fair to note that real `hash(-1)` is still `-1`, `-2` - [is the result](https://stackoverflow.com/a/7648531/1113207) of how it's implemented in a Python. – Mikhail Gerasimov Apr 06 '20 at 07:15