Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'example:': 0.03; 'cc:addr :python-list': 0.11; 'def': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'nan': 0.16; 'other:': 0.16; 'subclass': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'skip:p 40': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'comparing': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; '"",': 0.31; '>>>>': 0.31; 'keyerror:': 0.31; 'file': 0.32; 'class': 0.32; '(most': 0.33; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'false': 0.36; 'recent': 0.39; 'skip:n 10': 0.64; 'different': 0.65; 'jul': 0.74; 'subject:For': 0.78; 'float,': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=NpC4RrOD+3bA0uKeAJv/Egi1Du0u/I45xdJSb10xBcY=; b=Wu36Xpp1UgDdjKZ224UaVeUAiIoVUo5kdPTEqq/k+XLb+fl1pwHBzs5DB/37qlEoRK xhapDy+To1djilWMyVIwl8vFwVlZ52gfQmFFh3NIpJW2HE72W81DidCMrn5M0tzqx4Fc O16ccSYvACzHralnSKb8b5OBbs+BWEkVAADXU3F9OF2jL8yLbSPwyYIEaaib8msqe525 7lI5/4Wc1Wz7HMV8U9M4AhygasgJgzhesYTMAqBl8ATbpS06UX7HL0eovW4sONVVbhbl WVb4gQ1Fdsq0+Ckn/QCYfpFybEQqJd5GrEoctKRyDYuMZY/H8koTo0J31tOE+9+8SjSn gkYQ== MIME-Version: 1.0 X-Received: by 10.58.150.1 with SMTP id ue1mr34500433veb.11.1404842245840; Tue, 08 Jul 2014 10:57:25 -0700 (PDT) In-Reply-To: References: <53BC05FB.4050707@jmunch.dk> <878uo3akqy.fsf@elektro.pacujo.net> <871ttvahaq.fsf@elektro.pacujo.net> Date: Wed, 9 Jul 2014 03:57:25 +1000 Subject: Re: NaN comparisons - Call For Anecdotes From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404842254 news.xs4all.nl 2860 [2001:888:2000:d::a6]:38727 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74193 On Wed, Jul 9, 2014 at 3:54 AM, Chris Angelico 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 "", line 1, in > 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 "", line 1, in d[NoisyFloat("nan")] KeyError: nan That's comparing nan==nan three types with four different nans. ChrisA