weak reference to bound method

Ole Streicher ole-usenet-spam at gmx.net
Fri Oct 2 11:40:27 CEST 2009


Hi Thomas,

Thomas Lehmann <t.lehmann at rtsgroup.net> writes:
>> r = weakref.ref(o.myfunc)
>> print r()
>> >>>>   None
> 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 trying...

This is clear: in your case, o.myfunc is explicitely referenced by k,
this avoids the garbage collection.

My problem is that I have a class that delegates a function call, like:

--------------------8<------------------
import weakref

class WeakDelegator(object):
    def __init__(self, func):
        self._func = weakref.ref(func)

    def __call__(self):
        func = self._func()
        return func() if func else None
--------------------8<------------------

This does not work for bound methods because the weak reference to a
bound method will always point to None, even if the object still exists.

Why is that the case and how can I implement such a class properly?

Best regards

Ole



More information about the Python-list mailing list