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

George Burdell gburdell1 at
Fri Nov 12 23:03:33 CET 2010

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"?

More information about the Python-list mailing list