id() and is operator

Terry Reedy tjreedy at udel.edu
Mon Feb 23 01:02:13 EST 2015


On 2/22/2015 4:25 PM, Marko Rauhamaa wrote:
> LJ <luisjosenovoa at 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.

-- 
Terry Jan Reedy




More information about the Python-list mailing list