id

alister alister.ware at ntlworld.com
Thu Aug 24 14:09:01 EDT 2017


On Thu, 24 Aug 2017 11:21:27 -0400, Ned Batchelder wrote:

> On 8/24/17 10:42 AM, Stefan Ram wrote:
>> i = 0 while True: print( f"{ i }:{ id( i )}" ); i = i + 1
>>
>>   This loop prints increasing ids while i is less than 257, and then it
>>   starts to print alternating ids.
>>
>>   So this seems to indicate that temporary objects are created for
>>   large integers, and that we can observe that two (different?) objects
>>   (which do not exist simultaneously) can have "the same identity"?
>>
>>
> Correct.  Small integers are interned, and will always be the same
> object for the same value.  Ids can be re-used by objects which don't
> exist at the same time.  In CPython, id(x) is the memory address of x.
> When an object is freed, another object will eventually occupy the same
> memory address, and get the same id.
> 
> --Ned.

This is all implementation dependent



-- 
If someone says he will do something "without fail", he won't.



More information about the Python-list mailing list