[Python-ideas] Better support for finalization with weakrefs

Calvin Spealman ironfroggy at gmail.com
Wed Aug 1 22:56:56 CEST 2012

On Wed, Aug 1, 2012 at 12:27 PM, Richard Oudkerk <shibturn at gmail.com> wrote:
> On 01/08/2012 4:41pm, Lefavor, Matthew (GSFC-582.0)[MICROTEL LLC] wrote:
>> If finalizations are objects, is there any reason you can't make the
>> finalization object have an unregister method, like below? Or does this
>> not address the problem?
>>      >>> class Kenny: pass
>>      ...
>>      >>> kenny = Kenny()
>>      >>> f = finalize(kenny, print, "you killed kenny!")
>>      >>> f
>>      <finalize.finalize object at 0xffed4f2c>
>>      >>> f.unregister()
>>      >>> del kenny
>>      >>>
>> Matthew Lefavor
> I have a patch (with tests and docs) at
>     http://bugs.python.org/issue15528
> It has a pop() method for unregistering the callback.
> So if f was created using
>     f = finalize(obj, func, *args, **kwds)
> then f.pop() returns a tuple
>     (wr, func, args, kwds)
> where wr is a weakref to obj.
> There is also a get() method which returns the same info, but does
> not unregister the callback.
> Once the finalizer is dead, f(), f.get(), f.pop() all return None.

I don't like reusing the method names of containers, which this isn't.
I liked "peek" and "detach".

Also, why not fully hide the weakref adn return the actual object when
you call one fo them, but have them fail if the object no longer
exists? Or, what if they were just properties that looked up the
weakref'ed object on access but you could just get at the callback and
args directly?

> Richard
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

Read my blog! I depend on your acceptance of my opinion! I am interesting!
Follow me if you're into that sort of thing: http://www.twitter.com/ironfroggy

More information about the Python-ideas mailing list