[Python-Dev] == on object tests identity in 3.x - list delegation to members?

Ethan Furman ethan at stoneleaf.us
Mon Jul 14 04:55:37 CEST 2014


On 07/13/2014 08:13 AM, Andreas Maier wrote:
> Am 11.07.2014 22:54, schrieb Ethan Furman:
>>
>> Here is the externally visible behavior:
>>
>> Python 3.5.0a0 (default:34881ee3eec5, Jun 16 2014, 11:31:20)
>> [GCC 4.7.3] on linux
>> Type "help", "copyright", "credits" or "license" for more information.
>> --> NaN = float('nan')
>> --> NaN == NaN
>> False
>> --> [NaN] == [NaN]
>> True
>
> Ouch, that hurts ;-)

Yeah, I've been bitten enough times that now I try to always test code before I post.  ;)


> Test #8: Same object of class C
>     (C.__eq__() implemented with equality of x,
>      C.__ne__() returning NotImplemented):
>
>    obj1: type=<class '__main__.C'>, str=C(256), id=39406504
>    obj2: type=<class '__main__.C'>, str=C(256), id=39406504
>
>    a) obj1 is obj2: True
> C.__eq__(): self=39406504, other=39406504, returning True

This is interesting/weird/odd -- why is __eq__ being called for an 'is' test?

--- test_eq.py ----------------------------
class TestEqTrue:
     def __eq__(self, other):
         print('Test.__eq__ returning True')
         return True

class TestEqFalse:
     def __eq__(self, other):
         print('Test.__eq__ returning False')
         return False

tet = TestEqTrue()
print(tet is tet)
print(tet in [tet])

tef = TestEqFalse()
print(tef is tef)
print(tef in [tef])
-------------------------------------------

When I run this all I get is four Trues, never any messages about being in __eq__.

How did you get that result?

--
~Ethan~


More information about the Python-Dev mailing list