[PyKDE] Python references and the GC

Mikael Schönenberg micke at strakt.com
Wed Oct 30 12:28:00 GMT 2002


On Tue, 29 Oct 2002, Kaleb Pederson wrote:

> I have a class similar to the below that keeps a reference to itself so that
> it doesn't get garbage collected.

If my understanding of Python's garbage collection is what I hoped it was,
your assumption here is rather optimistic.

In essence, if I understand things correctly, the garbage collector is
"smart" in the way that it will identify cyclic references of otherwise
inaccessible objects. Beazley's "Python Essential Reference" illustrates
this with the following example:

a = {}
b = {}
a['b'] = b
b['a'] = a
del a
del b

Now, since the dictionaries refer to eachother, the reference count of
both is indeed greated than 0, but since the objects are not accessible
from any other namespace, the garbage collector will trash them anyhow.

Not digging further into the rest of your code, I believe that this is
what bit you. Keep a reference to the dialog elsewhere, and don't rely on
it staying alive by just having the line:

  self._keep = self

/micke




More information about the PyQt mailing list