[Python-ideas] Better support for finalization with weakrefs

Richard Oudkerk shibturn at gmail.com
Thu Aug 2 13:49:33 CEST 2012



On 01/08/2012 9:56pm, Calvin Spealman wrote:
> I don't like reusing the method names of containers, which this isn't.
> I liked "peek" and "detach".

Yeah, maybe so.

> 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?

I did consider that but worried about the "race" where the object is
garbage collected just after you pop from the registry.

Thinking about it again, you can just try resolving the weakref before
popping from the registry.

> 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?

The finalizer itself is just a light weight dictionary key.  It should
not own references to anything non-trivial.  Otherwise storing it could
keep alive big objects (or cause ref-cycles) even after the finalizer
is dead.

But all the information could be available from properties which return
None when the finalizer is dead.

Richard




More information about the Python-ideas mailing list