While analyzing id()
built-in behavior with numbers & string I got confusion.
x = 100
y = x # x and y both points to same object that is 100
id(x)
162569156
id(y)
162569156
Now I tried to change the object where x
is pointing as
x = 200 # since x points to different objects & garbage collector increments reference counts as its no longer points to old objects
id(y)
162569156
id(x) # its different
162569932
Now as of my understanding python garbage collector which is reference count garbage collector, the moment x=200
or number of references changes, it clean it up.
Here is my doubt, from above point can I assume that whenever I will do x = 100
again i.e tried to point to old object, id()
will give exactly same memory location always as previuosly ?
x = 100 #making x to points to same old object
id(x)
162569156 #getting same old location
id(y)
162569156
If above is true then does that means that for each number(millions) one references will be there and id()
gives same location when points to old object in future ?
I got this doubt as in other language like in C
once pointer lost its address(heap), its not guaranteed that next time it will get same address when you will do malloc
.