Re: A proposal to modify `None` so that it hashes to a constant
Chris Angelico <[email protected]> Mon, 5 Dec 2022 05:16:19 +1100
| Newsgroups | gmane.comp.python.devel |
|---|---|
| Message-ID | <CAPTjJmrwozZPMYSvzoBVcN_jr62ekP5WRN1LCB8jdvqPd7bY_A@mail.gmail.com> |
On Mon, 5 Dec 2022 at 05:11, Rob Cliffe via Python-Dev <[email protected]> wrote: > > Wild suggestion: > Make None.__hash__ writable. > E.g. > None.__hash__ = lambda : 0 # Currently raises AttributeError: > 'NoneType' object attribute '__hash__' is read-only Hashes have to be stable. If you change the hash of None after it's been inserted into a dictionary, you'll get all kinds of entertaining problems. >>> class X: ... def __init__(self): self.hash = 0 ... def __hash__(self): return self.hash ... >>> x = X() >>> d = {x: "This is x"} >>> x.hash = 1 >>> for key in d: print(key, key in d) ... <__main__.X object at 0x7f2d07c6f1c0> False ChrisA