assymetry between a == b and a.__eq__(b)

Mel Wilson mwilson at the-wire.com
Sat Dec 4 15:16:44 CET 2004


In article <zN6sd.505724$D255547 at attbi_s51>,
Steven Bethard <steven.bethard at gmail.com> wrote:
>Mel Wilson wrote:
>> In article <Qtqrd.177755$HA.59149 at attbi_s01>,
>> Steven Bethard <steven.bethard at gmail.com> wrote:
>>
>>>I believe what Peter Otten was pointing out is that calling __eq__ is
>>>not the same as using ==, presumably because the code for == checks the
>>>types of the two objects and returns False if they're different before
>>>the __eq__ code ever gets called.
>>
>>
>> Doesn't seem to:
>[snip]
>
>Hmm... maybe it only shows up with subclassing?

:) Seems to:


Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Eq(object):
...     def __eq__(self, other):
...             return True
...
>>> class Neq(Eq):
...     def __eq__(self, other):
...             print "(according to Neq)"
...             return False
...
>>> eq,neq=Eq(),Neq()
>>> eq==neq
(according to Neq)
False
>>> neq==eq
(according to Neq)
False
>>>



        Regards.        Mel.

















































More information about the Python-list mailing list