sets.Set doesn't honour __eq__

David Vaughan davidvau at eudoramail.com
Wed Jul 7 15:06:44 CEST 2004


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

a = caseless_string('a')
A = caseless_string('A')
print A == a

from sets import Set
assert len(Set([a,A])) == 1


If looks like Set is using dict.__setitem__.  In that
case, I reckon that the following is allowed to act in
a surprising fashion. Am I correct?


from sets import Set
foo = 3
bar = 3
if len(Set([foo,bar])) == 2:
    print 'Boo!'


Is there an implementation of Set around which checks
for equality, honouring __eq__ ?  Any help welcome.

David Vaughan



Need a new email address that people can remember
Check out the new EudoraMail at
http://www.eudoramail.com



More information about the Python-list mailing list