[Python-ideas] breaking cycles that include __del__

Adam Olsen rhamph at gmail.com
Mon Oct 19 18:34:38 CEST 2009

On Mon, Oct 19, 2009 at 08:25, Daniel Stutzbach
<daniel at stutzbachenterprises.com> wrote:
> On Sat, Oct 17, 2009 at 11:17 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> The gist is that you split your object into a "core" which requires
>> finalisation (but can never itself be caught up in a cycle) and the main
>> object which may participate in cycles. A weak reference to the main
>> object is placed in a global container. When the weak reference callback
>> is invoked, the object core is still in a valid state so it can be
>> finalised safely, even though the main object may be in the midst of
>> cycle collection.
>> This recipe gives a pretty good idea of how that approach works:
>> http://code.activestate.com/recipes/519610/
> That idiom (and that recipe in particular! yuck!) is pretty convoluted, IMO.

The critical idea is to list attributes that cannot be cleared,
leaving everything else to be cleared by default.  This is MUCH less
precarious than calling arbitrary code from within the GC, and
accomplishes nearly the same thing.

The use of a core further benefits the GC implementation, but not much
the user.  Obviously a recipe like that cannot show real benefit from
it, and I admit the recipe is pretty ugly.

Adam Olsen, aka Rhamphoryncus

More information about the Python-ideas mailing list