Message info From:Jim Jewett Subject:[issue9141] Allow objects to decide if they can be collected by GC Date:Thu, 05 Apr 2012 20:57:00 +0000

Jim Jewett <> added the comment:

I think the second patch is a fairly straightforward enhancement to the existing situation. It still feels a bit like an awkward half-measure, though.

(1) If a class does have a finalizer, instances should still be able to say "Go ahead and collect me, I don't happen to be in the problematic state." (In other words, there should be a return value that means not to bother checking for the existence of a tp_del / __del__ method.)

(2) It should be explicit whether or not this is:
(2a) just an inquiry ("If you were in a cycle, could I collect you?),
(2b) a statement plus inquiry ("You are in a cycle. Can I collect you?"), or
(2c) a request ("You are in a cycle, and I would like to collect you. Clean up if you can, then tell me whether you are still uncollectable.")

(3) It may be worth adding an additional slot for safe idempotent finalizers. (Earlier suggestions called this __close__, but I suppose __finalize__ might be more general.) Once a garbage cycle is detected, go ahead and call that slot's method before giving up and sticking the whole thing in garbage. If python classes could also fill this slot, it would look a lot like (2c).

nosy: +Jim.Jewett

Python tracker <>
Python-bugs-list mailing list