keying by identity in dict and set

Peter J. Holzer hjp-python at hjp.at
Mon Oct 28 04:14:47 EDT 2019


On 2019-10-20 19:31:43 +0200, Steve White wrote:
> The point is, I don't think __eq__() is ever called in a situation as
> described in my post,

This is only guaranteed if the hash is actually stored within the dict.
This seems to be the case in the CPython implementation, but there are
other ways to implement a hash table which don't store the hashes
explicitely (in fact, I don't think any of the variants we learned about
at university 30 years ago did this).

So your code may work with CPython, but not other Python implementations.
Worse, it may stop working with future CPython versions if the dict
implementation is changed.

But even if you are fine with that, by using a different equality
criterion for dicts than for everything else, your code is violating one
of every Python programmer's core assumptions. This will very likely trip
somebody up (maybe yourself in two years).

        hp

-- 
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp at hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20191028/98fef87a/attachment-0001.sig>


More information about the Python-list mailing list