Why is dictionary.keys() a list and not a set?

"Martin v. Löwis" martin at v.loewis.de
Sat Nov 26 07:56:33 EST 2005


Mike Meyer wrote:
>>class mylist1(list):
>>     def __hash__(self): return 0815
>>
>>class mylist2(list):
>>     def __hash__(self): return id(self)
>>
>>In the case of mylist1, everything is ok including semantics, but
>>performance suffers dramatically. In mylist2, performance is great,
>>but semantics suffers greatly.
>>Which of these user-defined types would you call "hashable"?
> 
> 
> The latter two, as the given __hash__ meets the specifications for
> __hash__.

This is not true. The second definition of __hash__ does not meet
the specifications:

http://docs.python.org/ref/customization.html

"The only required property is that objects which compare equal have the
same hash value"

However, I can have two instances of mylist2 which compare equal,
yet have different hash values.

Regards,
Martin



More information about the Python-list mailing list