WeakrefValueDictionary of Callables?

L

Lloyd Weehuizen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,298
Messages
2,571,539
Members
48,274
Latest member
HowardKipp

Latest Threads

Top