WeakrefValueDictionary of Callables?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lloyd Weehuizen

    #1

    WeakrefValueDictionary of Callables?

    Hey

    I'm trying to set up a WeakrefValueDic tionary 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.WeakVal ueDictionary()

    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.BindFunc tion( TEST_EVENT, self.TestFuncti on )

    def TestFunction():
    print "TestFuncti on OK"

    test_binder = TestBinder()
    test_case = TestCase( test_binder )

    test_binder.Cal lFunction( 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
Working...