Sets in Python
Bryan Olson
fakeaddress at nowhere.org
Sat Sep 22 01:17:50 EDT 2007
Gabriel Genellina wrote:
> En Thu, 20 Sep 2007 08:46:29 -0300, Steven D'Aprano
>
>> Another way is to use this class:
>>
>> class HashableList(list):
>> def __hash__(self):
>> return hash(tuple(self))
>
> ...and that will stop working as soon as the list is mutated (which is
> exactly what you said before)
Yup. I had suggested that technique in this thread, but it
doesn't really work. It hashes by state and compares by
state, but the actual key that a dict would store is the
object's identity. If the state has changed by the time
of a dict lookup, the dict will look in the hash-bucket
of the old state, but the object's equality test will
compare against the current state.
Bummer. Sorry.
--
--Bryan
More information about the Python-list
mailing list