Re: A proposal to modify `None` so that it hashes to a constant

Rob Cliffe via Python-Dev <[email protected]> Thu, 1 Dec 2022 22:18:49 +0000
Newsgroups gmane.comp.python.devel
Message-ID <[email protected]>
Wild suggestion:
     Make None.__hash__ writable.
E.g.
     None.__hash__ = lambda : 0 # Currently raises AttributeError: 
'NoneType' object attribute '__hash__' is read-only
Best wishes
Rob Cliffe

On 01/12/2022 11:02, Oscar Benjamin wrote:
> On Thu, 1 Dec 2022 at 06:56, Chris Angelico <[email protected]> wrote:
>> On Thu, 1 Dec 2022 at 17:26, Yoni Lavi <[email protected]> wrote:
>>> So it's not like it's even possible to require this generally for all objects.
>> Well, I mean, in theory you could require that objects whose hash
>> isn't otherwise defined get given the hash of zero. That doesn't
>> violate any of the actual rules of hashes, but it does make those
>> hashes quite suboptimal :)
>>
>> It's interesting how id() and hash() have opposite requirements (id
>> must return a unique number among concurrently-existing objects, hash
>> must return the same number among comparing-equal objects), yet a hash
>> can be built on an id.
> This also demonstrates a significant reason why None is special: it's
> a singleton that only compares equal to itself. The reason for using
> id for hash in other cases is to make different instances have
> different hashes but there is only ever one instance of None. A
> singleton class can have a hash function that matches identity based
> equality without using id: any constant hash function will do.
>
> --
> Oscar
> _______________________________________________
> 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/MTTJJN2HHP3A264DN3CAWSXITHRMLLUW/
> 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/XRASAGN52DAM7EAKJOYSWHJEKFAP2JPT/
Code of Conduct: http://python.org/psf/codeofconduct/