[Python-Dev] weakref gc semantics

James Y Knight foom at fuhm.net
Thu Nov 4 19:44:22 CET 2004


On Nov 2, 2004, at 11:40 PM, Tim Peters wrote:
> a reachable callback can have visible effects, and there
> really isn't ambiguity about which of c and W "dies first" (c does) in
> the example.

An unreachable callback function can also have visible effects.

> The question is whether gc should really be looking at whether the
> weakref *callback* is reachable, regardless of whether the weakref
> itself is reachable (if the weakref is reachable, its callback is too,
> and 2.4b2 invokes it -- the only case in question is the one in the
> example, where the weakref and the weakref's referent are unreachable
> but the weakref's callback is reachable).

 From what I'm hearing here, the following two pieces of code would then 
have different behavior under patch-callback:

"""
def C_gone(ignored):
     print "An object of type C went away."

class C:
     def __init__(self):
         self.wr = weakref.ref(self, C_gone)
"""

and

"""
class C:
     def __init__(self):
         def C_gone(ignored):
             print "An object of type C went away."
         self.wr = weakref.ref(self, C_gone)
"""

If that's correct, it sounds like a bad idea to me.

James



More information about the Python-Dev mailing list