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

Steven Bethard steven.bethard at gmail.com
Fri Dec 3 18:38:07 EST 2004


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?

 >>> class C(object):
...     def __eq__(self, other):
...         return True
...
 >>> class D(C):
...     def __eq__(self, other):
...         return False
...
 >>> c, d = C(), D()
 >>> c == 3
True
 >>> 3 == c
True
 >>> c == d
False
 >>> d == c
False
 >>> c.__eq__(d)
True
 >>> d.__eq__(c)
False


STeve



More information about the Python-list mailing list