I am doing object identification 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 '\nf - 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 throughout its entire life cycle, and that two objects with non-overlapping lifetimes can have the same id() value.
From this I understand that I can get the same id() at runtime for different variables, but I am surprised that in my code the same id() values ββalso match the values ββof the variables.
I mean that I always get the same id() value for var1=10 . The same thing happens with the assignment var1=1 , which has its own id() value. Even executing this task in different functions returns the same id() .
So my question is: Does Python keep a record of previous variables, values, and identifiers even after their expiration?
If the code has an assignment to a variable with the same value as a previously expired variable, does Python check the records of previous expired variables in memory and give priority to use the same id() for the same memory values?
I would like to learn a little about reusing id() values ββand memory management in a Python program.