Best practice for caching hash
Cameron Simpson
cs at cskk.id.au
Wed Mar 16 18:32:50 EDT 2022
On 16Mar2022 14:00, Marco Sulla <Marco.Sulla.Python at gmail.com> wrote:
>On Wed, 16 Mar 2022 at 00:42, Cameron Simpson <cs at cskk.id.au> wrote:
>> >In this case I currently cache the value -1. The subsequent calls to
>> >__hash__() will check if the value is -1. If so, a TypeError is
>> >immediately raised.
>>
>> This will also make these values behave badly in dicts/sets, as they all
>> hash to the same bucket.
>
>Not sure to understand. If the hash is -1, it's not hashable, so it
>can't be a member of a dict or set.
Sorry, I misread you and took -1 to be a valid hash code, not a signal
to raise TypeError.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Python-list
mailing list