From: MRAB on
F�lix-Antoine Fortin wrote:
> Thanks Gabriel, you resumed quite well what I did discovered after my
> second post
> by playing with the garbage collector module.
>
>> (The garbage collector will,
>> eventually, break the cycle and free those objects, but not very soon).
>
> I'm not very familiar with the Python garbage collector, so you may
> excuse my
> simple question, but how can it break the cycle? I guess the object
> will be
> freed at least when the program ends, but could it be before that? Is
> there a
> mechanisme in the garbage collector to detect circular references?
>
In CPython objects are reference-counted, which allows an object to be
collected as soon as there are no references to it.

However, this won't take care of circular references, so a secondary
garbage collector was introduced which occasionally looks for
inaccessible objects (garbage) using (probably) mark-and-sweep.