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

SourceForge.net noreply at sourceforge.net
Sun May 22 16:37:10 CEST 2005


Bugs item #1206537, was opened at 2005-05-22 14:37
Message generated for change (Tracker Item Submitted) made by Item Submitter
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: Open
Resolution: None
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


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

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