Re: A proposal to modify `None` so that it hashes to a constant
Rob Cliffe via Python-Dev <[email protected]> Thu, 8 Dec 2022 10:38:27 +0000
| Newsgroups | gmane.comp.python.devel |
|---|---|
| Message-ID | <[email protected]> |
You're right of course. Oh well, it *was* a wild idea.😁 Rob Cliffe On 04/12/2 On 04/12/2022 18:16, Chris Angelico wrote: > 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 > _______________________________________________ > Python-Dev mailing list -- [email protected] > To unsubscribe send an email to [email protected] > https://mail.python.org/mailman3/lists/python-dev.python.org/ > Message archived at https://mail.python.org/archives/list/[email protected]/message/OVVIKTG7CBN6BII4OBGIXWQJJXYCEO3I/ > Code of Conduct: http://python.org/psf/codeofconduct/ _______________________________________________ Python-Dev mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/L7JDJFPJKYNHIA7QVYGC74SYAVODM2IX/ Code of Conduct: http://python.org/psf/codeofconduct/