By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,793 Members | 1,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,793 IT Pros & Developers. It's quick & easy.

WeakrefValueDictionary of Callables?

P: n/a
Hey

I'm trying to set up a WeakrefValueDictionary of callables however as
soon as my method that adds the callable to the dictionary exits the
value is removed? Is there any way around this?

Example:
import weakref
TEST_EVENT = 1

class TestBinder:
def __init__( self ):
self.entries = weakref.WeakValueDictionary()

def BindFunction( self, event_id, function ):
self.entries[event_id] = function

def CallFunction( self, event_id, *args ):
self.entries[event_id]( *args )
class TestCase:
def __init__( self, binder ):
binder.BindFunction( TEST_EVENT, self.TestFunction )

def TestFunction():
print "TestFunction OK"

test_binder = TestBinder()
test_case = TestCase( test_binder )

test_binder.CallFunction( TEST_EVENT )

This generates a KeyError: 1, if I don't use weakrefs, then the TestCase
object is never cleaned up until TestBinder is destroyed.

Thanks
May 21 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.