I'm interested in knowing when (id(x), x) would be preferable over (type(x), x).

On Mon, 2020-12-21 at 17:52 +0000, David Mertz wrote:
On Mon, Dec 21, 2020 at 5:27 PM Christopher Barker <pythonchb@gmail.com> wrote:
Surely what you're looking for is some kind of typed hash table?

Maybe, maybe not. My impression is that the Typed hash  table is a kluge to get around this one issue. As others have pointed out, 1 and 1.0 are considered equal, and thus hash the same — a typed  hash table would not consider them the same. And while maybe useful, it would violate the spirit of duck typing. If you really want that kind of static typing, maybe Python is not the language for the job.


I've occasionally wanted an "identity set".  Except, after I want it, I pretty quickly always realize I don't actually want it.  Exactly what strings and integers will actually be interned, etc? There's a lot of implementation dependent stuff there that isn't a clear semantics (I mean, it's knowable, but too complex).

In any case, I know how to write that in a few lines if I feel like having it.  Basically just use pairs `(id(x), x)` in a set.  That's more specific than `(type(x), x)`, although both have their own gotchas.
 

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/PWFX7WBNRQN5UBT27R2SOUBMUOXGVF6P/
Code of Conduct: http://python.org/psf/codeofconduct/