How safe is a set of floats?
Peter Otten
__peter__ at web.de
Fri May 4 14:24:10 EDT 2007
Paul McGuire wrote:
> Just to beat this into the ground, "test for equality" appears to be
> implemented as "test for equality of hashes". So if you want to
> implement a class for the purposes of set membership, you must
> implement a suitable __hash__ method. It is not sufficient to
> implement __cmp__ or __eq__, which I assumed "test for equality" would
> make use of. Not having a __hash__ method in my original class caused
> my initial confusion.
As with dictionaries, only items with the same hash are considered for
equality testing.
> So would you suggest that any class implemented in a general-purpose
> class library should implement __hash__, since one cannot anticipate
> when a user might want to insert class instances into a set? (It
> certainly is not on my current checklist of methods to add to well-
> behaved classes.)
A meaningful implementation would also have to make sure that the attributes
used to calculate hash and equality don't change over time.
No, I wouldn't bother because YAGNI.
Peter
More information about the Python-list
mailing list