On Wed, Jul 9, 2014 at 3:54 AM, Chris Angelico <ros...@gmail.com> wrote: >>>> class X: > def __eq__(self, other): > if self is other: > print("Comparing against self - I am me!") > return True > print("Comparing against",other,"-",id(other)) > return False > def __hash__(self): > return 0 > >>>> d[X()] > Comparing against nan - 18777952 > Comparing against nan - 19624864 > Comparing against nan - 18776272 > Traceback (most recent call last): > File "<pyshell#20>", line 1, in <module> > d[X()] > KeyError: <__main__.X object at 0x016B40D0>
Better example: Subclass float, then it can actually *be* a nan. >>> class NoisyFloat(float): def __eq__(self, other): print("Comparing",id(self),"against",id(other)) return super().__eq__(other) def __hash__(self): return super().__hash__() >>> d[NoisyFloat("nan")] Comparing 23777152 against 18777952 Comparing 23777152 against 19624864 Comparing 23777152 against 18776272 Traceback (most recent call last): File "<pyshell#35>", line 1, in <module> d[NoisyFloat("nan")] KeyError: nan That's comparing nan==nan three types with four different nans. ChrisA -- https://mail.python.org/mailman/listinfo/python-list