[ python-Bugs-1206537 ] weakref cannot handle bound methods (in contrast to docu)

SourceForge.net noreply at sourceforge.net
Tue Jun 14 11:42:28 CEST 2005


Bugs item #1206537, was opened at 2005-05-22 09:37
Message generated for change (Settings changed) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1206537&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Extension Modules
Group: None
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Raik Gruenberg (graik)
>Assigned to: Nobody/Anonymous (nobody)
Summary: weakref cannot handle bound methods (in contrast to docu)

Initial Comment:
According to the documentation of the weakref module,
weakreferences can be applied to
"...class instances, functions written in Python (but
not in C), methods (both bound and unbound)..."

In reality, bound methods cannot be referenced (see bug
813299):

import weakref

## this works:
def testF( event ):    pass
r = weakref.ref( testF )

## this doesnt:
class EventListener:
    def handleEventA( self, event ):
        pass

t = EventListener()
## gives a "dead" ref
r = weakref.ref( t.handleEventA )

This behaviour is unexpected for anyone not aquainted
to the inner clockwork of python and unfortunate
because it, for example, prevents to keep weak
references to callback methods in event handling
patterns.  

A workaround is proposed at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81253

Discussion:
(minimal) Solution 1:
Change the weakref documentation

(preferred) Solution 2:
Adapt weakref to allow references to bound methods

Perhaps this bug should be converted into a
documentation bug and a feature request.

Python version 2.3 and 2.4
OS: Linux 2.6


----------------------------------------------------------------------

Comment By: Armin Rigo (arigo)
Date: 2005-05-23 09:08

Message:
Logged In: YES 
user_id=4771

Of course, the weakref documentation is ultimately right,
and the problem, unrelated to bound methods, is that you
always get a dead weakref if you do

    weakref.ref(<expr-than-returns-a-new-object>)

But I'm not being particularly helpful here.

A trick simpler than the cookbook proposals is to force the
method object to be alive as long as the instance itself by
storing it on the instance:

    obj = MyClass()
    obj.m = obj.m
    ref = weakref.ref(obj.m)

This works because the last "obj.m" returns an existing
object, as opposed to one created just-in-time.  This
might be mentioned in the weakref documentation with the
comment that it's a general rule to be careful not to take
weakrefs to short-lived object; the same problem would
occur e.g. when taking a weakref to "obj.a" where "a" is
a computed property.  Storing the result back on "obj" --
under the same or another name -- is a workaround.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1206537&group_id=5470


More information about the Python-bugs-list mailing list