pre-PEP generic objects

Peter Otten __peter__ at web.de
Tue Nov 30 04:37:19 EST 2004


Steven Bethard wrote:

> def __eq__(self, other):
>     """x.__eq__(y) <==> x == y"""
>     return (isinstance(other, self.__class__)
>             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

With indirect use of __eq__() this puzzling behaviour disappears:

>>> B() == Bunch()
False
>>> Bunch() == B()
False

Whether this is intended, I don't know. If someone can enlighten me...

In any case I would prefer self.__class__ == other.__class__ over
isinstance().

Peter
 




More information about the Python-list mailing list