[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022年12月04日 10:19:47 -0800

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/

Reply via email to