[Python-ideas] incremental hashing in __hash__

jab at math.brown.edu jab at math.brown.edu
Fri Dec 30 20:10:14 EST 2016


On Fri, Dec 30, 2016 at 8:04 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

> No.  It is possible to have two keys be equal but different -- an easy
> example is 1 and 1.0; they both hash the same, equal the same, but are not
> identical.  dict has to check equality when two different objects hash the
> same but have non-matching identities.
>


Python 3.6.0 (default, Dec 24 2016, 00:01:50)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {1: 'int', 1.0: 'float'}
>>> d
{1: 'float'}


IPython 5.1.0 -- An enhanced Interactive Python.

In [1]: class Foo:
   ...:     def __eq__(self, other):
   ...:         return True
   ...:     def __init__(self, val):
   ...:         self.val = val
   ...:     def __repr__(self):
   ...:         return '<Foo %r>' % self.val
   ...:     def __hash__(self):
   ...:         return 42
   ...:

In [2]: f1 = Foo(1)

In [3]: f2 = Foo(2)

In [4]: x = {f1: 1, f2: 2}

In [5]: x
Out[5]: {<Foo 1>: 2}



I'm having trouble showing that two equal but nonidentical objects can both
be in the same dict.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161230/0b41b81e/attachment.html>


More information about the Python-ideas mailing list