From: Vincent van Beveren on
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.vanBeveren(a)rijnhuizen.nl

From: Peter Otten on
Vincent van Beveren wrote:

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

The instance doesn't keep a reference of its bound method. Rather the bound
method keeps a reference of its instance. Every time you say

a.b

you get a different bound method. What do you think should keep it alive?


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

I fear you have to manage the methods' lifetime explicitly.

Peter
From: Vincent van Beveren on
Hi Peter,

I did not know the object did not keep track of its bound methods. What advantage is there in creating a new bound method object each time its referenced? It seems kind of expensive.

Regards,
Vincent

-----Original Message-----
From: Peter Otten [mailto:__peter__(a)web.de]
Sent: vrijdag 30 juli 2010 15:06
To: python-list(a)python.org
Subject: Re: The untimely dimise of a weak-reference

Vincent van Beveren wrote:

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

The instance doesn't keep a reference of its bound method. Rather the bound
method keeps a reference of its instance. Every time you say

a.b

you get a different bound method. What do you think should keep it alive?


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

I fear you have to manage the methods' lifetime explicitly.

Peter

From: Peter Otten on
Vincent van Beveren wrote:

> I did not know the object did not keep track of its bound methods. What
> advantage is there in creating a new bound method object each time its
> referenced? It seems kind of expensive.

While I didn't measure it I suppose that it saves a lot of memory.

Peter
From: Christian Heimes on
Am 30.07.2010 16:06, schrieb Vincent van Beveren:
> I did not know the object did not keep track of its bound methods. What advantage is there in creating a new bound method object each time its referenced? It seems kind of expensive.

Instances of a class have no means of storing the bound method object.
The or unbound bound method is a simple and small wrapper that keeps a
reference to the class, "self" and the function object. Python keeps a
pool of empty method objects in a free list. The creation of a new bound
method just takes a few pointer assignments and three INCREFs.

Christian