[Python-Dev] Re: weakref gc semantics

Michael Hudson mwh at python.net
Fri Nov 5 12:48:16 CET 2004

Tim Peters <tim.peters at gmail.com> writes:

[stuff i admittedly haven't thought terribly hard about]

> The former is accurate independent of whether we're talking about
> cyclic trash now, but that latter would not be.  For example,
> def callback(ignore):
>     print 'hi'
> class C:
>     pass
> c = C()
> w = weakref.ref(c, callback)
> w = None
> c = None
> The callback outlives c, but the callback doesn't get invoked, because
> w didn't outlive c.  The callback does get invoked if the order of the
> last two lines is swapped.  It's easy to explain this in terms of
> which of {c, w} goes away first, but not in terms of callback's
> lifetime.

As disclaimed above, I haven't thought about this a great deal, but I
would expect it to be the lifetime of the weakref which matters -- in
the not-all-that-unlikley case of the callback not being a lambda or
local function, it is *always* going to be reachable, and indeed is
never going to die until the process exits...

I take it the changes you're proposing aren't going to change the
semantics of the quoted code?


  I'm about to search Google for contract assassins to go to Iomega
  and HP's programming groups and kill everyone there with some kind
  of electrically charged rusty barbed thing.
                -- http://bofhcam.org/journal/journal.html, 2002-01-08

More information about the Python-Dev mailing list