python - Is the `id()` of a variable associated to the value assigned to it after the variable lifetime finishes? -
i tinkering objects' identity in python following code:
def f(var1): print 'within f , before modification: var1= '+str(var1)+', id= '+str(id(var1)) var1 = 10 print 'within f , after modification: var1= '+str(var1)+', id= '+str(id(var1)) def f2(var1): print 'within f , before modification: var1= '+str(var1)+', id= '+str(id(var1)) var1 = 1 print 'within f , after modification: var1= '+str(var1)+', id= '+str(id(var1)) def f3(var1): print 'within f , before modification: var1= '+str(var1)+', id= '+str(id(var1)) var1 = 10 print 'within f , 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 , before modification: var1= 5, id= 18089816 within f , 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 , before modification: var1= [4, 3, 1, 6], id= 23884720 within f , 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 , before modification: var1= 7, id= 18089768 within f , 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 , before modification: var1= a, id= 140350777144584 within f , after modification: var1= 1, id= 18089912 after function: var= a, id= 140350777144584
i understand identity of object guaranteed unique during lifetime, , 2 objects non-overlapping lifetimes may have same id()
value.
from understand can same id()
during execution of code different variables, surprised in code same id()
values coincide variable values.
i mean same id()
value var1=10
. same happens assignment var1=1
has own id()
value. doing assignment in different functions returns same id()
.
so question is: python keeping record of previous variables, values , identities after lifetimes have expired?
if in code there variable assignment same value expired variable, python check records of previous expired variables in memory , give priority use same id()
same memory values?
i understand little bit more id()
values reuse , memory management in python program.
short answer question - python caches integers in range [-5 , 256] .
so whenever var1 = 10
or var1 = 1
, same object integer cache, , why seeing same id
in different runs of function.
if try out values greater or equal 257, may able see different results.
a simple example of behavior -
>>> var1 = 257 >>> var2 = 257 >>> id(var1) 36635792 >>> id(var2) 36636304 >>> var1 = 10 >>> var2 = 10 >>> id(var1) 1642386016 >>> id(var2) 1642386016
Comments
Post a Comment