id

Ned Batchelder ned at nedbatchelder.com
Thu Aug 24 11:21:27 EDT 2017


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.



More information about the Python-list mailing list