Thanks for that, I had a feeling that was the problem. Is there anyway 
around this? I'd prefer not to have to assume the method name on the object.

This could be solved by passing the object and method name in as two 
separate parameters to the Bind function. But ideally I'd like to 
extract this information somehow from the bound method and give the 
external API a more natural feel. Is that possible?

Steve Holden wrote:
 > Lloyd Weehuizen wrote:
 >> 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?
 >
 >
 > I believe your problem is that the bound method references aren't
 > being retained (i.e. referenced) anywhere else. A bound method is an
 > object its own right, and can cease to exist at any time -
 > particularly in your
 > case when the weak reference is the only reference to it!
 >
 > Consider that the following code actually appears to work:
 >
 >import weakref
 >TEST_EVENT = 1
 >
 >class TestBinder:
 >         def __init__(self):
 >                 self.entries = weakref.WeakValueDictionary()
 >
 >         def BindFunction(self, event_id, obj):
 >                 self.entries[event_id] = obj
 >
 >         def CallFunction(self, event_id, *args):
 >                 self.entries[event_id].TestFunction(*args)


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to