assymetry between a == b and a.__eq__(b) (WAS: pre-PEP genericobjects)

Terry Reedy tjreedy at udel.edu
Wed Dec 1 16:07:09 EST 2004


"Steven Bethard" <steven.bethard at gmail.com> wrote in message
 >>>def __eq__(self, other):
>>>    """x.__eq__(y) <==> x == y"""
>>>    return (isinstance(other, self.__class__)

Since an instance of a subclass is an instance of a parent class, but not 
vice versa, I believe you introduce here the assymetry you verify below.

>>>            and self.__dict__ == other.__dict__)
>> This results in an asymmetry:
>>
>>>>>from bunch import Bunch
>>>>>class B(Bunch): pass
>> ...
>>>>>B().__eq__(Bunch())
>> False
>>>>>Bunch().__eq__(B())
>> True

Terry J. Reedy






More information about the Python-list mailing list