weak reference to bound method

Ole Streicher ole-usenet-spam at gmx.net
Fri Oct 2 04:54:11 EDT 2009


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



More information about the Python-list mailing list