weak reference to bound method

ryles rylesny at gmail.com
Sat Oct 3 17:37:00 EDT 2009


On Oct 2, 4:54 am, Ole Streicher <ole-usenet-s... at gmx.net> wrote:
> Hi group,
>
> 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?
>
> Is this a bug or a feature? (Python 2.6)
>
> Best regards
>
> Ole

Have a look at: http://mindtrove.info/articles/python-weak-references



More information about the Python-list mailing list