sets.Set doesn't honour __eq__

Matteo Dell'Amico della at toglimi.linux.it
Wed Jul 7 09:40:35 EDT 2004


David Vaughan wrote:
> I was expecting the class sets.Set to act like an
> unordered list with no two members equal.  But, while
> the following code prints True, the assertion fails.
> 
> 
> from sets import Set
> 
> _base = str
> class caseless_string(_base):
>     """Strings, but equality ignores case."""
>     def __eq__(self, other):
>         return _base.__eq__(self.upper(), other.upper())
> [...]

You just have to define __hash__ too:

class caseless_string(str):
     """Strings, but equality ignores case."""
     def __eq__(self, other):
         return str.__eq__(self.lower(), other.lower())
     def __hash__(self):
         return str.__hash__(self.lower())

works fine for me.

(btw, why do you do the _base trick?)

-- 
Ciao,
Matteo



More information about the Python-list mailing list