3 ms·
You can write a custom hash method that returns -1 (and the one you wrote verifiably does.) The standard (not custom) hash function, in CPython, which calls yo
by dragonwriter 2y ago
You can write a custom hash method that returns -1 (and the one you wrote verifiably does.)
The standard (not custom) hash function, in CPython, which calls your custom method and returns the actual value CPython uses for bucketing, will return -2 in that case, though.
Which is necessary, so that, e.g., following the single mathematical function for hashing of numeric types published in the Python Language Reference will preserve x==y implies hash(x)==hash(y) for custom and build in numeric types in CPython where hash(-1) does not follow that pattern.
Even though for -1 itself, the divergence from the rule is in __hash__ itself. This is also consistent with the basic rule that dunder methods are for communication with the standard machinery, but external consumers should use the standard machinery, not the dunder method.