On Thu, 31 Jul 2014 20:12:12 -0700, Dan Stromberg wrote: > I removed some quotes, and noticed that 1 and 1.0 hash the same. That's > a bit unexpected, but I suppose it's not completely unreasonable.
You should expect that two equal objects should hash to the same value in different versions of Python, or even from one run of Python to the next. But within a single session, then far from being "not completely unreasonable", having equal items hash to the same value is the only reasonable thing to do. It is part of the dict API that if x == y, then dict[x] == dict[y], and that cannot happen unless x and y hash to the same thing. In general, it's not entirely possible to enforce that, especially given how flexible custom __eq__ methods can be, so Python doesn't even try. But with *numbers* (or at least those which are part of the numeric tower), Python does insist that each distinct value should hash to the same result no matter what type that value happens to be: py> n = 23 py> hash(n) == hash(long(n)) == hash(float(n)) == hash(complex(n)) True py> from fractions import Fraction py> from decimal import Decimal py> hash(n) == hash(Fraction(n)) == hash(Decimal(n)) True With the possible exception of Decimal, which is not fully integrated with the numeric tower, all numbers in the standard library will obey the condition that if x == y, hash(x) == hash(y), regardless of type. -- Steven -- https://mail.python.org/mailman/listinfo/python-list