Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:not': 0.03; 'cpython': 0.05; 'initialize': 0.07; 'memory.': 0.07; "subject:' ": 0.07; 'string': 0.09; 'bug.': 0.09; 'integers': 0.09; 'runtime': 0.09; 'cc:addr:python-list': 0.11; 'martin': 0.11; 'assume': 0.14; '"@"': 0.16; '"is': 0.16; '"is"': 0.16; 'confuse': 0.16; 'from:addr:pobox.com': 0.16; 'from:addr:skip': 0.16; 'identifiers': 0.16; 'none.': 0.16; 'otoh,': 0.16; 'sentinel': 0.16; 'tuple': 0.16; 'value"': 0.16; 'values:': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; '(the': 0.22; '>>>': 0.22; 'example': 0.22; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'integer': 0.24; "shouldn't": 0.24; 'skip': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'included': 0.31; 'comparison': 0.31; 'object.': 0.31; "can't": 0.35; 'created': 0.35; 'definition': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'data,': 0.36; 'false': 0.36; 'detail': 0.37; 'example,': 0.37; 'list.': 0.37; 'represent': 0.38; 'whatever': 0.38; 'itself': 0.39; 'system.': 0.39; 'simply': 0.61; 'first': 0.61; 'guarantee': 0.63; 'to:addr:gmail.com': 0.65; '257': 0.84; 'defeat': 0.91; 'mistake': 0.91; 'obligation': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=UnnaaUU1+WNd+RD/zdLXvkCorvHokUlMVm6z6RlFxiw=; b=eGqiWE87D+6G5/17Ps/lxD0OkqrbGCyb6GQMtJyCDAc85cBmwdTRDXnh9Jz6GSBCp+ SLGCGTi+/k98vTtCHnaWA2y/0NanW8mr/qDR67TwLmv3KD6OpICLJI5wktaZf2y9kHvc kUzLKWoL+e0ZBW2ZbRYVXfJyCqTpvGGiMqXYhBiQgboVb15Lj2qFP6B9dpwyaZA2GZTr 8WeqyxrJEoS2BvY894erGSdqLw/3PrxHymwIkNTXcjIHlsIxs37QSmjKeyjeC34DBE1c +bzeeNH5uEEcuN9YdYt7GRyz6sOBx1NxFUdqn24LNovUTaoDStz3PJ85+09UynR2ghGJ S9ig== MIME-Version: 1.0 X-Received: by 10.50.142.97 with SMTP id rv1mr5646562igb.13.1408453955481; Tue, 19 Aug 2014 06:12:35 -0700 (PDT) Sender: skip.montanaro@gmail.com In-Reply-To: References: <87fvgt7c4i.fsf@elektro.pacujo.net> <53F27340.90604@stoneleaf.us> Date: Tue, 19 Aug 2014 08:12:35 -0500 X-Google-Sender-Auth: CJHwFed9GshJ4PYuT_5gSQTss5Q Subject: Re: 'is not' or '!=' From: Skip Montanaro To: Martin S Content-Type: text/plain; charset=UTF-8 Cc: "python-list@python.org" 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: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1408453963 news.xs4all.nl 2907 [2001:888:2000:d::a6]:56609 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:76564 On Tue, Aug 19, 2014 at 7:42 AM, Martin S wrote: >> For example, in CPython 3.4.1: >>>>> (254 + 3) is 257 >> False >>>>> (254 + 3) == 257 >> True >>>>> ('asd' + '@sd') is 'asd@sd' >> False >>>>> ('asd' + '@sd') == 'asd@sd' >> True > > Now you have managed to confuse this newbie: What would a valid > "is-example" look like? Perhaps adding to the confusion: >>> (4 + 3) is 7 True The use of "is" or "is not" is the right thing to do when the object of the comparison is known to be a singleton. That is true for None. (I suspect it's true for True and False as well, though for historical and idiomatic reasons "x is True" is never used.) It would also be true if you created a sentinel object like this: SENTINEL = [] then used it to represent "no valid value" in situations where you can't otherwise assume some value like None is outside the domain of values: if mything is not SENTINEL: # do whatever is necessary to initialize mything ... The case of "(4 + 3) is 7" simply exposes an optimization in the implementation of integers in CPython (the small integer cache), and *must not be relied on*. Note that SENTINEL = () would be a mistake if () is in the domain of your data, since the empty tuple is itself a singleton. That is a CPython implementation detail you shouldn't rely on: >>> x = () >>> >>> y = (1,2,3) >>> >>> z = y[0:0] >>> z () >>> x () >>> x == z True >>> x is z True That's why the first definition of SENTINEL uses the empty list. I can guarantee that there is no other object in the system which "is" that particular object. (Strings which look like identifiers are also "interned" so there is only one copy in memory. I suspect that's why Chris's string example included an "@" - to defeat that optimization.) The CPython runtime is under no obligation to retain these optimizations. Relying on them is a bug. OTOH, CPython guarantees there is only one None object in the system. Skip