Deleting objects with method reference

Martin martin at tv-animation.com
Thu Apr 15 05:59:27 EDT 2004


Peter Otten <__peter__ at web.de> wrote in message news:<c5iusn$v32$03$1 at news.t-online.com>...
> ...
> You can verify this with a weakref.ref object with an appropriate callback:
> 
> import weakref
> def ondel(o):
>     print "del"
> 
> class B:
>    def F(self):
>       print "f"
>    def Set(self):
>       self.v = self.F
> 
> 
> o1 = B()
> o1.Set()
> w = weakref.ref(o1, ondel)
> del o1
> 
> Peter

Hi Peter and thanks for your reply!

However your example doesn't seem to work.

import weakref
import gc

def ondel(o):
   print "del"

class B:
   def F(self):
      print "f"
   def Set(self):
      self.v = self.F

o1 = B()
w1 = weakref.ref(o1, ondel)
del o1
>>> del

o2 = B()
o2.Set()
w2 = weakref.ref(o2, ondel)
del o2

When I delete o2 I get no output. And gc.garbage is still empty!

gc.garbage
>>> []

I have tried it in both python 2.1 and 2.3.3. Why doesn't it work?

-Martin



More information about the Python-list mailing list