weak reference to bound method

Peter Otten __peter__ at web.de
Fri Oct 2 11:58:23 CEST 2009

Ole Streicher wrote:

> 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?


from weakref import ref

class A(object):
    def f(self): return "f"

class Method(object):
    def __init__(self, obj, func=None):
        if func is None:
            func = obj.im_func
            obj = obj.im_self
        self._im_self = ref(obj)
        self._im_func = ref(func)
    def __call__(self):
        obj = self._im_self()
        func = self._im_func()
        if obj is not None and func is not None:
            return func.__get__(obj)()

a = A()
m = Method(a.f)
print m()
del a
print m()

It's still not clear to me why you would want to do that...


More information about the Python-list mailing list