Newbie (read: pot. stupid) question on objects/namespaces

Stephan Houben stephan at pcrm.win.tue.nl
Tue Aug 31 11:33:47 EDT 1999


fredp at multimania.com.nospam (Fred Pacquier) writes:

> stephan at pcrm.win.tue.nl (Stephan Houben) said :

> >Note that in *both* cases you create a circular reference which means
> >that CPython cannot reclaim the class Bar or the instance of Baz.
> 
> Duh. Is this a total "no-no", or what ? If this is only for a handful of 
> instances that are supposed to persist through the entire program lifetime, 
> is it okay to explicitely del() them at the end or not ?

No, this was just a word of caution!
If it's just a handful of instances, don't bother.

However, if you want to get rid of them, del'ling them doesn't work.
You must break the circularity to get them garbage collected.
In my original example:

> >class Baz:
> >    def __init__(self):
> >        self.x = MyClass(self)

,when you have created an instance:

baz = Baz()

,you can break the circularity by either doing:

baz.x = None

or

baz.x.owner = None

But don't do this in the __del__() method of Baz!
That's not good enough. You have to do this in order to
get __del__() called in the first place.

Now, if you use JPython, you have real garbage collection, and
all this is a non-issue.

Greetings,

Stephan




More information about the Python-list mailing list