weak reference to bound method

Thomas Lehmann t.lehmann at rtsgroup.net
Fri Oct 2 11:27:43 CEST 2009

> I am trying to use a weak reference to a bound method:
> class MyClass(object):
>     def myfunc(self):
>         pass
> o = MyClass()
> print o.myfunc
> >>>>   <bound method MyClass.myfunc of <__main__.MyClass object at 0xc675d0>>
> import weakref
> r = weakref.ref(o.myfunc)
> print r()
> >>>>   None
> This is what I do not understand. The object "o" is still alive, and
> therefore the bound method "o.myfunc" shall exists.
> Why does the weak reference claim that it is removed? And how can I hold
> the reference to the method until the object is removed?

k = o.myfunc
r = weakref.ref(k)
print r()
>>>>>  <weakref at 00B80750; to 'method' at 00B59918 (myfunc)>

Don't ask me why! I have just been interested for what you are

More information about the Python-list mailing list