CPython's cyclic garbage collector (was [Python-ideas] Automatic context managers)

Chris Angelico rosuav at gmail.com
Fri Apr 26 22:20:46 EDT 2013


On Sat, Apr 27, 2013 at 12:12 PM, Skip Montanaro <skip at pobox.com> wrote:
> From the Zen of Python:
>
>> In the face of ambiguity, refuse the temptation to guess.
>
> I believe the reason something isn't already done to break cycles is
> that the authors of the cyclic garbage collector considered the above
> aphorism.  They rely on the author of the code with the cycles to
> figure out how to break them.  All I was suggesting was that Python
> could provide a hook where the programmer could codify his algorithm
> for breaking cycles.

Sure, and that makes good sense. The hook would be an improvement
(maybe it gets passed a list of garbage that's about to be added to
gc.garbage, but will be re-checked for cycles after the hook, so all
you have to do is break the cycle in the hook and it'll work), but the
implication is that an external piece of code knows about the possible
cycles and how to deal with them. I'd really rather have something
right there in the class. Effectively, something like this:

whenever gc.garbage has content:
    turn_into(gc.garbage[0],None) # or a special "I am destroyed" object
    del gc.garbage[:]

It doesn't matter which one gets taken out. Yes, I suppose that's a
form of guessing, but whatever rules get put in place - whether by the
language or by your own script - it'll always be possible to conjure a
scenario where tossing a coin is the only way to pick which one goes.

ChrisA



More information about the Python-list mailing list