why is self not passed to id()?

Robert Kern robert.kern at gmail.com
Thu Sep 4 23:22:51 CEST 2008


Maric Michaud wrote:
> Le Thursday 04 September 2008 22:26:53 Ruediger, vous avez écrit :

>> class foo(list):
>>     __hash__ = lambda x: id(x)
> 
> Wow ! You are really going on trouble with this, believe me there is a real 
> good reason for list not to be hashable. A dictionnary or set containing some 
> of your foo is virtually inconsistent, read carefully the manual about 
> prerequesites for dict keys, they *need* to be immutable.

Well, that's not entirely true. They need to be not mutated while they are in 
the dictionary, certainly. At least not in ways that affect equality testing. In 
this case, one would also have to override list.__eq__ to also compare by 
identity, too. Then you could mutate the lists to your heart's content and the 
dictionary wouldn't care.

-- 
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