On Thu, 1 Dec 2022 at 17:26, Yoni Lavi
the language makes no guarantee about hash consistency between executions
because it's futile in the general case, even if objects were to get a serial `id` and hash by it for example, any change in the number of objects created across all of Python (including its builtin modules and various libraries unrelated to the user code) would make these hashes move.
For the record, Jython DOES use sequential numbers for ids. And it doesn't reuse them even if the objects are disposed of.
id(None) 2 lists = [[] for _ in range(10)] [id(l) for l in lists] [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] lists = None lists = [[] for _ in range(10)] [id(l) for l in lists] [13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
IIRC an id is not assigned to an object until one is requested.
So it's not like it's even possible to require this generally for all objects.
Well, I mean, in theory you could require that objects whose hash isn't otherwise defined get given the hash of zero. That doesn't violate any of the actual rules of hashes, but it does make those hashes quite suboptimal :) It's interesting how id() and hash() have opposite requirements (id must return a unique number among concurrently-existing objects, hash must return the same number among comparing-equal objects), yet a hash can be built on an id.
Besides, do other languages require it? Is it required for the language to behave in a manner that makes sense?
Or maybe you think it's by pure accident that such an overwhelming majority of languages and software libraries implement/use deterministic hashing functions for primitive types or aggregates that consist of such types? I can't figure out if you think it's actually a bad property for the language to have, or really just arguing that it's bad for the sake of it.
Determinism is usually the easiest option. True randomness takes a lot of effort compared to a deterministic PRNG, hence web servers having true entropy but old game consoles relying on PRNGs. Whether determinism is fundamentally good or fundamentally bad depends heavily on context. Question: Is the metaquestion "is determinism good" deterministic (ie can it be answered entirely from predictable facts), or is it itself entropic? I believe the former, but I'm curious if anyone disagrees! ChrisA