python - identifying objects, why does the returned value from id(...) change? -
id (object)
This integer (or long integer) is a guarantee that your During this lifetime, this object should be unique and stable.
Can you explain this output? Why does the ID of j
change?
& gt; & Gt; & Gt; I = 10> and gt; & Gt; ID (i) 6337824 & gt; & Gt; & Gt; J = 10> and gt; & Gt; ID (J) 6337824 & gt; & Gt; & Gt; J = j + 1> and gt; & Gt; ID (J) 6337800 & gt; & Gt; & Gt; ID (i) 6337824 itemprop = "text"> Because integers are irreversible, each integer value is a specific object, with a unique ID, the integer 10 < / Code> has a different ID than 11
. The value of an existing integer object does not change to j = j + 1
, rather to point it to j
on the object for 11
Changes .
See that when we independently create a new variable k
and value it 11
:
& Gt; & Gt; & Gt; J = 10> and gt; & Gt; ID (J) 8402204 & gt; & Gt; & Gt; J = j + 1> and gt; & Gt; ID (J) 8402192 & gt; & Gt; & Gt; K = 11> gt; & Gt; & Gt; ID (K) 8402192
Note that this is not always the case that there is one and only one such object in every integer. This is only for small integers which decides the Python cache. This does not happen for large integers:
& gt; & Gt; & Gt; X = 123456789> & gt; & Gt; & Gt; ID (x) 8404568 & gt; & Gt; & Gt; Y = 123456789> & gt; & Gt; & Gt; See ID (Y) 8404604
:
For all integers in the current implementation, the integer objects of -5 and 256 are placed when you enter that range You can actually just get the reference of the current object back.
Comments
Post a Comment