473,492 Members | 4,279 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

WeakrefValueDictionary of Callables?

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
0 903

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3792
by: Kasper B. Graversen | last post by:
Having played with Smalltalk for the past month, I'm getting used to passing code as arguments to methods... how easy is it to do this in python? I haven't seen any recent postings on this subject...
39
6007
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
699
33308
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
2
1142
by: Gabriel B. | last post by:
Is it just me that can't find a full reference in the docs? I wanted a list of all the methods of dict for example... where can i find it? Thanks, and sorry if this question is just dumb, i...
6
1724
by: Daishi Harada | last post by:
Hi, I'm trying to find/write a memoizing decorator that works both for functions and methods. I've been looking at the following two samples: ...
3
1358
by: Guyon Morée | last post by:
I have a function with some params including some data to be used with re.sub and some other data which i want to use in the replacement. I can provide re.sub with a callable, but this will only...
6
1337
by: Russell Warren | last post by:
Is there any better way to get a list of the public callables of self other than this? myCallables = classDir = dir(self) for s in classDir: attr = self.__getattribute__(s) if callable(attr)...
18
2681
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
4
1313
by: georg.heiss | last post by:
Hello, with Python it is simple to call direct functions from c-librarys. Is there a way to browse over a library i.e. '/lib/libc.so' with python, to see all possible functions in a library? ...
0
7118
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5452
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1397
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.