Newbie question about python garbage collection when keeping only a reference to an object's member

Robert Kern robert.kern at gmail.com
Fri Nov 12 23:12:23 CET 2010


On 11/12/10 4:03 PM, George Burdell wrote:
> My understanding is that any object which is not pointed to by any
> variable will be automatically deleted. What if I create a class
> object, but only keep a reference to one of its members, and not a
> reference to the object itself? What goes on internally in Python?
> Does Python retain the whole object, or does it just keep a copy of
> the referenced member?
>
> For example, if I have
>
> def myclass:
>      def __init__(self):
>         self.x = [1,2,3]
>         self.y = [4,5,6]
> x = myclass().x
>
> This works, and I correctly get x = [1,2,3]. But what happened to the
> myclass() object initially created, and the member "y"?

They get deleted.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list