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

G

George Burdell

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

Robert Kern

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
 
S

Steve Holden

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

The myclass() call created a myclass instance (which contains a
reference to a newly created list, which therefore has a refcount of 1).

Attribute access to that instance then returns a second reference to the
list, which is bound to the name x in the current local namespace (or
the module global namespace if this is top-level code). The binding
results in a reference count of two for the list.

The instance, having no outstanding references, is then
garbage-collected, which means that the objects referenced by its
namespace have their reference counts decremented for each reference. So
the destruction of the myclass instance reduces the reference count of
the list [1, 2, 3] to one, and that of the list [4, 5, 6] to zero (which
causes *it* to be garbage collected).

And you are left with a local variable x that references a list whose
current contents are [1, 2, 3].

Remember, the new values aren't created in local namespace. They are
created in a shared area of memory (often referred to as the "heap"), so
it's quite easy to "keep objects alive" that were created inside
functions - just make sure you hold on to references outside the
functions, and you are fine.

regards
Steve
 
E

Emile van Sebille

On 11/12/2010 2:03 PM George Burdell said...
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:

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

Garbage collect(ed|able)

It only existed (was referenceable) while instantiated and can be gc'd
one no longer referenced.

Emile
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top