3 ms·
Oh, while we're talking about things with predictable hashes, here's a Python WTF that I enjoy: class A: def __hash__(self): return -1
by rspeer 11y ago
Oh, while we're talking about things with predictable hashes, here's a Python WTF that I enjoy:
class A:
def __hash__(self):
return -1
print(hash(A()))
Prints -2.
For the same reason, if you ever need an easy hash collision, use the hashes of -1 and -2.
- rspeer 11y agoI'll explain the reason, BTW: a hash value of -1 isn't valid in CPython because that integer is reserved as a flag value. So any hash method that returns -1 quietly gets its value changed to -2.