Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #86195

Re: id() and is operator

Date 2015-02-22 22:29 -0800
From Gary Herron <gherron@digipen.edu>
Subject Re: id() and is operator
References <87f18c68-120d-44f2-bd34-6f73c69365da@googlegroups.com> <871tlhiqpz.fsf@elektro.pacujo.net> <mcefpa$e0g$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.19055.1424672996.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 02/22/2015 10:02 PM, Terry Reedy wrote:
> On 2/22/2015 4:25 PM, Marko Rauhamaa wrote:
>> LJ <luisjosenovoa@gmail.com>:
>>
>>>>>> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

id() and is operator LJ <luisjosenovoa@gmail.com> - 2015-02-22 09:53 -0800
  Re: id() and is operator Laura Creighton <lac@openend.se> - 2015-02-22 19:13 +0100
  Re: id() and is operator Chris Angelico <rosuav@gmail.com> - 2015-02-23 06:23 +1100
  Re: id() and is operator Gary Herron <gherron@digipen.edu> - 2015-02-22 11:16 -0800
  Re: id() and is operator Laura Creighton <lac@openend.se> - 2015-02-22 20:58 +0100
  Re: id() and is operator Marko Rauhamaa <marko@pacujo.net> - 2015-02-22 23:25 +0200
    Re: id() and is operator Chris Angelico <rosuav@gmail.com> - 2015-02-23 08:36 +1100
    Re: id() and is operator Terry Reedy <tjreedy@udel.edu> - 2015-02-23 01:02 -0500
      Re: id() and is operator Marko Rauhamaa <marko@pacujo.net> - 2015-02-23 08:31 +0200
    Re: id() and is operator Gary Herron <gherron@digipen.edu> - 2015-02-22 22:29 -0800
  Re: id() and is operator Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-23 12:31 +1100
  Re: id() and is operator Terry Reedy <tjreedy@udel.edu> - 2015-02-23 01:14 -0500

csiph-web