__eq__() inconvenience when subclassing set

Mick Krippendorf mad.mick at gmx.de
Wed Oct 28 23:07:15 EDT 2009


Jess Austin schrieb:
> >>> frozenset([1]) == mySet()
> False
> 
> frozenset doesn't use mySet.__eq__() because mySet is not a subclass
> of frozenset as it is for set.

You could just overwrite set and frozenset:

class eqmixin(object):
    def __eq__(self, other):
        print "called %s.__eq__()" % self.__class__
        if isinstance(other, (set, frozenset)):
            return True
        return super(eqmixin, self).__eq__(other)

class set(eqmixin, set):
    pass
class frozenset(eqmixin, frozenset):
    pass
class MySet(set):
    pass


Regards,
Mick.



More information about the Python-list mailing list