I was just playing with id() function in iPython console. So let's say we have variables foo and bar:
foo = 2
id(foo)
Out[38]: 2007329888
bar = 4
id(bar)
Out[40]: 2007329952
So, unsurprisingly, foo and bar have different ids. And if we change bar to have a value of 2 both variables will now have the same id:
bar = 2
id(bar)
Out[42]: 2007329888
id(foo)
Out[43]: 2007329888
Ok, now we change both variables to have some other value. Now id 2007329888 is not associated with any variable:
foo = bar = True
id(foo)
Out[45]: 2006839456
id(bar)
Out[46]: 2006839456
But then if we assign a new variable a value of 2, it will get linked to that same old id:
bla = 2
id(bla)
Out[48]: 2007329888
So, my questions from all this are:
- How does Python decide when a value associated with an id is no longer needed and can be written over or are all ids and associated value kept until the end of the session?
- Is it possible to manually specify and overwrite value associated with an id? (I'm pretty sure the answer is a 'no', but just want to check.)
Thanks!