Hi, First, I come from a Java background. Ok, so I have this idea that I want to create an EventBus...basically a central class where objects can register themselves as listeners for different events. This central class also has methods so that objects can fire events. Something like this...
Say we have object MrTree and object MotherNature. MrTree listens for EVENT_GROW_LEAVES. So, when MrTree "hears" about EVENT_GROW_LEAVES, MrTree will do just that, grow some leaves. So, while our python app is running, MotherNature needs to tell MrTree to grow leaves. I am suggesting something like this... 1. MrTree has a method, growLeaves() 2. MrTree registers for the event, EventBus.register(MrTree, EVENT_GROW_LEAVES) 3. MotherNature fires grow leaves, EventBus.fire(EVENT_GROW_LEAVES) 4. MrTree's growLeaves method gets called by EventBus. Now, in Java I would make EventBus a Singleton so that only one instance of it would exist. And I would have MrTree implement EventListener interface, where the interface defines a method like growLeaves(). So when MotherNature tells the EventBus to fire the event, EventBus loops through it's list of listeners and calls, growLeaves() How would I go about doing this in Python? Here is my quick idea class EventListener: def eventOccurred(self, eventType): pass class MrTree(EventListener): def __init__(self): EventListener.__init__(self) EventBus.register(self, EventBus.EVENT_GROW_LEAVES) def eventOccurred(self, eventType): if eventType == EventBus.EVENT_GROW_LEAVES: print "Growing Leaves" class EventBus: EVENT_GROW_LEAVES = 0 __listeners = {} def register(listener, eventType): if __listeners.has_key(eventType): curListeners = __listeners.get(eventType) curListeners.append(listener) else: __listeners[eventType] = [listener] def fire(eventType): if __listeners.has_key(eventType): x = __listeners.get(eventType) for l in x: l.eventOccurred(eventType) class MotherNature: def doSomeStuff(): print "doing whatever Mother Nature does..." EventBus.fire(EventBus.EVENT_GROW_LEAVES) ...so that is what I am thinking. However, i guess my issue is how to make EventBus a singleton or prevent it from being instaniated and making it's methods statically accessible. thanks. -- http://mail.python.org/mailman/listinfo/python-list