4 ms·
interestingly enough though, it doesn't seem to be possible to write a custom hash function that returns -1 >>> class Foo: def __hash__(self):
by dagw 2y ago
interestingly enough though, it doesn't seem to be possible to write a custom hash function that returns -1
>>> class Foo:
def __hash__(self):
return -1
>>> f = Foo()
>>> print(hash(f))
-2
So if you're doing something where you have a custom __hash__ function that you're expecting to return -1 for a certain value and then are testing for value of the hash of an object rather than testing the property of the object directly, then this might bite you. But I cannot think of any reasonable case where you might want to do that.
- omoikane 2y agoPerhaps the worry was that because the observed behavior up until now was that hash never returns -1, if it were possible possible to write a custom hash function that returns -1, that might break some unsuspecting code due to Hyrum's Law.
- dragonwriter 2y agoIf the built-in hash function returned -1 when a custom __hash__ method did so, then custom numeric classes following the hashing guidelines in the Python Language Reference designed to maintain the x==y => hash(x)==hash(y) invariant would fail to maintain that invariant on the reference implementation of Python due to a CPython implementation detail, which would be a Bad Thing™.
- dragonwriter 2y agoYou 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.