Weakly-bound methods: use this decorator to create methods that weakly-reference their instance (im_self). This means that the method itself will not keep the object alive. Useful for callbacks, caches, and avoiding cyclic references.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | from weakref import proxy
from types import MethodType
class weakmethod(object):
__slots__ = ["func"]
def __init__(self, func):
self.func = func
def __get__(self, obj, cls):
if obj is not None:
obj = proxy(obj)
return MethodType(self.func, obj, cls)
#######################################################
>>> class Foo(object):
... @weakmethod
... def bar(self, a, b):
... print self, a, b
...
>>> f = Foo()
>>> b = f.bar
>>> b
<bound method Foo.bar of <weakproxy at 009FA1E0 to Foo at 009FC070>>
>>> b(1, 2)
<__main__.Foo object at 0x009FC070> 1 2
>>> del f
>>> b
<bound method Foo.bar of <weakproxy at 009FA1E0 to NoneType at 1E1D99B0>>
>>> b(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in bar
ReferenceError: weakly-referenced object no longer exists
|
there's been an old recipe doing something similar at: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81253 but i feel it over-complicates the task.
see http://sebulba.wikispaces.com/recipe+weakmethod for updates
Useful!!