Hi everyone,

I was working with weak references in Python, and noticed that it was 
impossible to create a weak-reference of bound methods. Here is a little python 
3.0 program to prove my point:

import weakref

print("Creating object...")
class A(object):
    
    def b(self):
        print("I am still here")
        
a = A()

def d(r):
    print("Aaah! Weakref lost ref") 

print("Creating weak reference")

r = weakref.ref(a.b, d)

print("Oh, wait, its already gone!")
print("Ref == None, cause of untimely demise: %s" % r())
print("Object is still alive: %s" % a)
print("Function is still exists: %s" % a.b)
print("See:")
a.b()

I also tried this in Python 2.5 and 2.6 (with minor modifications to the syntax 
of course), and it yielded the exact same behavior. Why is this, and is there 
anything I can do about it? I wish to reference these bound functions, but I do 
not want to keep them in memory once the object they belong to is no longer 
referenced.
 
Regards,
Vincent van Beveren

___
Ing. V. van Beveren
Software Engineer, FOM Rijnhuizen
E: v.vanbeve...@rijnhuizen.nl

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to