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

Christoph Zwerschke cito at online.de
Sat Nov 26 22:39:00 CET 2005


>>> class mylist1(list):
>>>     def __hash__(self): return 0815
>>>
>>> class mylist2(list):
>>>     def __hash__(self): return id(self)
>>>
>>> Which of these user-defined types would you call "hashable"?

 > Mike Meyer wrote:

>> The latter two, as the given __hash__ meets the specifications for
>> __hash__.

Martin v. Löwis wrote:

> This is not true. The second definition of __hash__ does not meet
> the specifications:
> "The only required property is that objects which compare equal have the
> same hash value"

As Mike has written in his last posting, you could easily "fix" that by 
tweaking the equality relation as well. So technically speaking, Mike is 
probably right. It would completely break common-sense semantics, 
because for mylist2, if I have a=[1] and b=[1] then I will have a!=b. 
This would not be very reasonable, but probably allowed by Python.

-- Christoph



More information about the Python-list mailing list