Unexpected behavior with dictionary keys containment and a user-defined class

Rob Clewley rob.clewley at gmail.com
Mon Jan 12 23:13:49 EST 2009


>> Hi, the short version of my question is: when is a dictionary's
>> __contains__ method behavior different to using the 'in' idiom?
>> (because I have an example of a difference in my code).
>
> Never.

Yes, sorry, I managed to summarize the long version incorrectly :)

> Well, the only conclusion is that dict uses the hash of an object to test
> containment while lists use id.

Great, thanks. I think a copy of my object has been made so that it
doesn't hash to the same thing. I'll write a __hash__ as suggested in
the other post. I forgot all about dictionary hashing for some
reason...

-Rob



More information about the Python-list mailing list