Rich comparison methods don't work in sets?

Robert Kern robert.kern at
Sat Jun 20 19:32:50 EDT 2009

On 2009-06-20 18:22, Terry Reedy wrote:
> Gustavo Narea wrote:
>> Hello again, everybody.
>> Thank you very much for your responses. You guessed right, I didn't
>> use the __hash__ method (and I forgot to mention that, sorry).
>> And unfortunately, I think I can't make them hashable, because the
>> objects are compared based on their attributes, which are in turn
>> other kind of objects compared based on other attributes. All these
>> class instances are compared with __eq__/__ne__ and they wrap
>> relatively complex data which would be hard to attempt to represent
>> them unambiguously using a 32-bit integer. That's why I'm afraid I
>> cannot use hashables.
> If the result of 'o1 == o2' changes over time, then the objects are not
> very suitable as set members.

Sometimes, I think it would be nice if sets and the other containers could be 
modified to accept a user-supplied hash and comparison functions on 
construction. A key function like list.sort() would probably also work. Even 
when you have objects that aren't hashable in general, there are many times when 
you know you will not be modifying them in a given context. Being able to do set 
operations on them in that context would be really useful.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

More information about the Python-list mailing list