Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!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.027 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'python.': 0.02; 'subject:not': 0.03; 'cpython': 0.05; "subject:' ": 0.07; 'string': 0.09; 'objects,': 0.09; 'cc:addr:python-list': 0.11; 'wrote:': 0.18; '>>>': 0.22; 'aug': 0.22; 'cc:addr:python.org': 0.22; '>>>': 0.24; 'comparing': 0.24; 'integer': 0.24; 'mon,': 0.24; 'versions': 0.24; 'cc:2**0': 0.24; 'header:In-Reply- To:1': 0.27; 'testing': 0.29; 'chris': 0.29; 'message- id:@mail.gmail.com': 0.30; '(which': 0.31; 'received:209.85.212': 0.32; 'received:209.85': 0.35; 'objects': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'object,': 0.36; 'example,': 0.37; 'wrong': 0.37; 'two': 0.37; 'received:209': 0.37; 'easily': 0.37; 'skip:& 10': 0.38; 'pm,': 0.38; 'anytime': 0.60; 'skip:2 20': 0.60; 'most': 0.60; 'such': 0.63; 'different': 0.65; 'due': 0.66; 'answer.': 0.68; 'behavior': 0.77; '257': 0.84; 'ethan': 0.84; 'furman': 0.84; 'careful': 0.91 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=5B/nR6YdiIx0Hq/vsERNx1XRCiAASBEdo3GDsK29IJc=; b=Jr9sQyGs/IyXDWt5AzSldk/PTYZKM+vyTK2ma3O4Aer2UDsZBi6QmjhFRmmqWXiODH ndz/+5Ox+re49Ld5Xln9KtMFaTt3nHSJW5kTY1HrD8zvYhrsFvIm184YBtiwyZ23hUX+ fOPZTXdnla7lPrYWuFP4qa8Z9rn1Nk7fTpeMeFSR6BK/1nkgXTz9vWbWVtkDSOUOSYEs JU+CawnCspceYXyzSepk1l4pUfrPVS+6KXFJbXsmTfjEJjX+pGPIwwTxKA5evXM44zd1 7CxH6K7GTOSYsgLUshTddM+em9dTZ/i8aQtKOK6FaEBjnYgmPcUR4LGLpDRv+F7JC8W1 RHfA== X-Gm-Message-State: ALoCoQn/eF8MP3QPob7S9OnSUtOlJNDIAgzN6LnvgioqmivZYDIa9JRcZ1ITGvqVsIgFzeguAVre X-Received: by 10.180.38.39 with SMTP id d7mr1982453wik.24.1408399488352; Mon, 18 Aug 2014 15:04:48 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <53F27340.90604@stoneleaf.us> References: <87fvgt7c4i.fsf@elektro.pacujo.net> <53F27340.90604@stoneleaf.us> From: Chris Kaynor Date: Mon, 18 Aug 2014 15:04:28 -0700 Subject: Re: 'is not' or '!=' To: Ethan Furman Content-Type: multipart/alternative; boundary=e89a8f646fcf349b150500ee8fee 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: 90 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1408401295 news.xs4all.nl 2856 [2001:888:2000:d::a6]:43121 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:76522 --e89a8f646fcf349b150500ee8fee Content-Type: text/plain; charset=UTF-8 On Mon, Aug 18, 2014 at 2:42 PM, Ethan Furman wrote: > If you are not dealing with singletons (which is most cases), such as > numbers, strings, lists, and most other arbitrary objects, you will need to > use "!=" or anytime the two objects you are comparing are not the exact > same object, you can easily get the wrong answer. 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 However, when testing these cases, you need to be careful due to optimizations like smaller integer interning and string interning: >>> (254 + 2) == 256 True >>> (254 + 2) is 256 True >>> ('asd' + 'sd') == 'asdsd' True >>> ('asd' + 'sd') is 'asdsd' True In each of these cases, the behavior may be different in other implementations or versions of Python. Chris --e89a8f646fcf349b150500ee8fee Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

= On Mon, Aug 18, 2014 at 2:42 PM, Ethan Furman <ethan@stoneleaf.us>= wrote:
If you are not dealing with singletons (which is most case= s), such as numbers, strings, lists, and most other arbitrary objects, you = will need to use "!=3D" or anytime the two objects you are compar= ing are not the exact same object, you can easily get the wrong answer.

For example, in CPython 3.4.1:
>>> (254 + 3) is 257
False
>>> (254 + 3) = =3D=3D 257
True
>>> ('asd' + '@sd') is 'asd@s= d'
False
>>> ('asd' + '@sd') =3D=3D 'asd@sd'
True

<= div class=3D"gmail_extra">However, when testing these cases, you need to be= careful due to optimizations like smaller integer interning and string int= erning:

>>> (254 + 2) =3D=3D 256
True
>>> (254 + 2) is 256<= /div>
True
>&= gt;> ('asd' + 'sd') =3D=3D 'asdsd'
True
>>> ('= asd' + 'sd') is 'asdsd'
True

<= div class=3D"gmail_extra">In each of these cases, the behavior may be diffe= rent in other implementations or versions of Python.

Chris
--e89a8f646fcf349b150500ee8fee--