[Tutor] Object references and garbage collection confusion

Dave Angel davea at davea.name
Tue May 5 21:09:16 CEST 2015


On 05/05/2015 12:29 AM, Brandon D wrote:
> Hello tutors,
>
> I'm having trouble understanding, as well as visualizing, how object
> references work in the following situation.  For demonstration purposes I
> will keep it at the most rudimentary level:
>
> x = 10
>
> x = x ** x
>
> If my knowledge serves me correctly, Python destroys the value once
> reassigned.  So, how does x = x +  1 work if it's destroyed before it can
> be referenced?  The only solution I came up with is that the two operands
> are evaluated before storing it in the variable, consequently replacing the
> original value of 0.

It's not destroyed before it's referenced.  The ten-object is destroyed 
(or may be, depending on optimizations and such) when nothing is bound 
to it.  That happens just after the new object is bound to x.

it may be interesting to put some simple statements into a function, and 
use dis.dis on that function.

import dis
def myfunc():
     x = 10
     x = x ** x

dis.dis(myfunc)



-- 
DaveA


More information about the Tutor mailing list