Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!81.171.88.16.MISMATCH!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4a.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python.': 0.02; 'cpython': 0.05; '(python': 0.07; 'explanation': 0.09; 'integers': 0.09; 'satisfy': 0.09; 'wrong,': 0.09; 'python': 0.11; '"=="': 0.16; '"is"': 0.16; '201': 0.16; '4:25': 0.16; 'caches': 0.16; 'examples:': 0.16; 'exists,': 0.16; 'integers.': 0.16; 'quite.': 0.16; 'reedy': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'example': 0.22; 'separate': 0.22; 'header:User-Agent:1': 0.23; 'integer': 0.24; 'compare': 0.26; 'references': 0.26; 'somewhere': 0.26; 'header :In-Reply-To:1': 0.27; 'correct': 0.29; 'generally': 0.29; 'gary': 0.31; 'object.': 0.31; 'could': 0.34; 'created': 0.35; 'objects': 0.35; 'but': 0.35; 'false': 0.36; 'detail': 0.37; 'two': 0.37; 'mine': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'according': 0.40; 'even': 0.60; 'simple': 0.61; "you've": 0.63; 'charset:windows-1252': 0.65; 'side': 0.67; 'between': 0.67; 'containing': 0.69; 'institute': 0.72; 'received:204': 0.75; 'dr.': 0.77; '2014,': 0.84; 'bitten': 0.84; 'experiment': 0.84 Date: Sun, 22 Feb 2015 22:29:50 -0800 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: id() and is operator References: <87f18c68-120d-44f2-bd34-6f73c69365da@googlegroups.com> <871tlhiqpz.fsf@elektro.pacujo.net> In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit 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: 76 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424672996 news.xs4all.nl 2829 [2001:888:2000:d::a6]:50801 X-Complaints-To: abuse@xs4all.nl X-Received-Body-CRC: 1596455287 X-Received-Bytes: 4810 Xref: csiph.com comp.lang.python:86195 On 02/22/2015 10:02 PM, Terry Reedy wrote: > On 2/22/2015 4:25 PM, Marko Rauhamaa wrote: >> LJ : >> >>>>>> id(b[0]) >>> 45855552 >> [...] >>>>>> id(b[2]) >>> 45855552 > >>> Please correct me if I am wrong, but according to this b[2] and b[0] >>> are the same object. Now, >>> >>>>>> b[0] is b[2] >>> False >> >> This is a true statement: >> >> If X is Y, then id(X) == id(Y). >> >> However, this is generally not a true statement: >> >> If X is Y, then id(X) is id(Y). > > If X and Y exist at the *same time*, then (X is Y) == (id(X) is > id(Y)). Since X and Y in the example above do not exist at the same > time, it is nonsensical to compare them. Not quite. You've been bitten by the "is" versus "==" trap. You could use id(X)==id(Y) but not id(X) is id(Y) not even if X and Y are the same object. Simple examples: >>> a=3 >>> id(a) is id(a) False >>> a=3 >>> b=a >>> id(a) is id(b) False The explanation is that each call to id() makes its own independent Python integer object containing the large integer (10771264 in this case). The two integer objects satisfy "==", but they are separate Python objects so they do not satisfy "is". As a side note, It is an implementation detail whether two Python integer objects created independently but with the same value are separate objects or references to a single object. CPython caches small integers so that only one integer object of each value exists, but not so for large integers. You can experiment with the cutoff on your particular flavor of Python. On mine (Python 3.4.2 (default, Oct 8 2014, 13:08:17) ;[GCC 4.9.1] on linux) it's somewhere between 200 and 300: >>> 201 is 1+200 True >>> 301 is 1+300 False Gary Herron -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418