Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; '"""': 0.07; 'doctest': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'type,': 0.09; 'def': 0.12; 'assume': 0.14; '*almost*': 0.16; '*containing*': 0.16; 'above?': 0.16; 'behave': 0.16; 'finney': 0.16; 'hypothetical': 0.16; 'implies': 0.16; 'nans': 0.16; 'permits': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subclass': 0.16; "type's": 0.16; 'type)': 0.16; 'starts': 0.20; '>>>': 0.22; 'header:User- Agent:1': 0.23; '(such': 0.24; 'membership.': 0.24; "i've": 0.25; 'developers': 0.25; 'define': 0.26; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'correct': 0.29; "doesn't": 0.30; 'membership': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'writes:': 0.31; 'class': 0.32; 'agreed': 0.32; 'everyone': 0.33; 'implemented': 0.33; 'core': 0.34; 'subject: (': 0.35; 'anybody': 0.35; 'but': 0.35; 'false': 0.36; 'i.e.': 0.36; 'method': 0.36; 'subject:?': 0.36; 'so,': 0.37; 'being': 0.38; 'expected': 0.38; 'ben': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'users': 0.40; 'skip:n 10': 0.64; 'equals': 0.68; 'other.': 0.75; '\xe2\x80\x93': 0.77; 'you:': 0.81; 'skip:_ 50': 0.83; '"spam"': 0.84; 'received:125': 0.84; 'skip:\xe2 50': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: Is nan in (nan,) correct? Date: Fri, 06 Mar 2015 13:55:32 +1100 References: <54f90c53$0$12994$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:HSVmOUYcglQzpwQnbPhD+BHddQA= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 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: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1425610573 news.xs4all.nl 2854 [2001:888:2000:d::a6]:40136 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86975 Steven D'Aprano writes: > Since reflexivity is *almost* universal, and using object identity > permits very substantial optimizations, the core developers agreed > that built-in contain types may assume that `x is y` implies `x == y`. > Users of NANs and other non-reflexive types can subclass or define > their own membership function. On a type (such as a hypothetical SQL NULL type) which does not have reflexivity – i.e. that ‘(x is x) == (x == x)’ may be False – which method needs to be implemented so items *containing* values of that type will have the expected semantics? I can only think of ‘footype.__contains__’, but that's a method of the *container* type, and the ‘in’ operator doesn't consult that method of the items themselves. So, given the hypothetical NullType:: class NullType(object): """ A type whose value never equals any other. This type's values will behave correctly when tested for membership in a collection:: >>> foo = NullType() >>> bar = NullType() >>> foo is foo True >>> foo is bar False >>> foo == foo False >>> foo == bar False >>> quux = [foo, "spam"] >>> "spam" in quux True >>> foo in quux True >>> bar in quux False """ def __eq__(self, value): return False def __method_which_the_in_operator_interrogates__(self, collection): """ Method which the ‘is’ operator interrogates for membership. """ return is_a_member_of(container, self) What method of NullType replaces the hypothetical ‘__method_which_the_in_operator_interrogates__’, which I've implemented to as you describe “define their own membership function”, in order to get the correct behaviour in the doctest above? -- \ “Why am I an atheist? I ask you: Why is anybody not an atheist? | `\ Everyone starts out being an atheist.” —Andy Rooney, _Boston | _o__) Globe_ 1982-05-30 | Ben Finney