I am tinkering with objects' identity in Python with the following code:
def f(var1):
print 'Within f and BEFORE modification: var1= '+str(var1)+', id= '+str(id(var1))
var1 = 10
print 'Within f and AFTER modification: var1= '+str(var1)+', id= '+str(id(var1))
def f2(var1):
print 'Within f and BEFORE modification: var1= '+str(var1)+', id= '+str(id(var1))
var1 = 1
print 'Within f and AFTER modification: var1= '+str(var1)+', id= '+str(id(var1))
def f3(var1):
print 'Within f and BEFORE modification: var1= '+str(var1)+', id= '+str(id(var1))
var1 = 10
print 'Within f and AFTER modification: var1= '+str(var1)+', id= '+str(id(var1))
var = 5
print '\n f - var1=10:'
print 'BEFORE FUNCTION CALL: var= '+str(var)+', id= '+str(id(var))
f(var)
print 'AFTER FUNCTION: var= '+str(var)+', id= '+str(id(var))
print '\n f2 - var1=1:'
var = [4,3,1,6]
print 'BEFORE FUNCTION CALL: var= '+str(var)+', id= '+str(id(var))
f2(var)
print 'AFTER FUNCTION: var= '+str(var)+', id= '+str(id(var))
print '\n f3 - var1=10 again:'
var = 7
print 'BEFORE FUNCTION CALL: var= '+str(var)+', id= '+str(id(var))
f3(var)
print 'AFTER FUNCTION: var= '+str(var)+', id= '+str(id(var))
print '\n f2 - var1=1 again:'
var='a'
print 'BEFORE FUNCTION CALL: var= '+str(var)+', id= '+str(id(var))
f2(var)
print 'AFTER FUNCTION: var= '+str(var)+', id= '+str(id(var))
Output:
f - var1=10:
BEFORE FUNCTION CALL: var= 5, id= 18089816
Within f and BEFORE modification: var1= 5, id= 18089816
Within f and AFTER modification: var1= 10, id= 18089696
AFTER FUNCTION: var= 5, id= 18089816
f2 - var1=1:
BEFORE FUNCTION CALL: var= [4, 3, 1, 6], id= 23884720
Within f and BEFORE modification: var1= [4, 3, 1, 6], id= 23884720
Within f and AFTER modification: var1= 1, id= 18089912
AFTER FUNCTION: var= [4, 3, 1, 6], id= 23884720
f3 - var1=10 again:
BEFORE FUNCTION CALL: var= 7, id= 18089768
Within f and BEFORE modification: var1= 7, id= 18089768
Within f and AFTER modification: var1= 10, id= 18089696
AFTER FUNCTION: var= 7, id= 18089768
f2 - var1=1 again:
BEFORE FUNCTION CALL: var= a, id= 140350777144584
Within f and BEFORE modification: var1= a, id= 140350777144584
Within f and AFTER modification: var1= 1, id= 18089912
AFTER FUNCTION: var= a, id= 140350777144584
I understand that the identity of an object is guaranteed to be unique during the its lifetime, and that two objects with non-overlapping lifetimes may have the same id()
value.
From that I understand that I can get the same id()
during the execution of the code for different variables, but I am surprised that in my code the same id()
values coincide also with the variable values.
I mean that I always get the same id()
value for var1=10
. The same happens with the assignment var1=1
that has its own id()
value. Even doing this assignment in different functions returns the same id()
.
So my question is: Is Python keeping a record of previous variables, values and identities even after their lifetimes have expired?
If in the code there is a variable assignment with the same value as a previously expired variable, does Python check records of the previous expired variables in memory and give priority to use the same id()
for the same memory values?
I would like to understand a little bit more about id()
values reuse and the memory management in a Python program.