We use an EventHook as suggested from Michael Foord in his Event Pattern:
Just add EventHooks to your classes with:
class MyBroadcaster()
def __init__():
self.onChange = EventHook()
theBroadcaster = MyListener()
# add a listener to the event
theBroadcaster.onChange += myFunction
# remove listener from the event
theBroadcaster.onChange -= myFunction
# fire event
theBroadcaster.onChange.fire()
We add the functionality to remove all listener from an object to Michaels class and ended up with this:
class EventHook(object):
def __init__(self):
self.__handlers = []
def __iadd__(self, handler):
self.__handlers.append(handler)
return self
def __isub__(self, handler):
self.__handlers.remove(handler)
return self
def fire(self, *args, **keywargs):
for handler in self.__handlers:
handler(*args, **keywargs)
def clearObjectHandlers(self, inObject):
for theHandler in self.__handlers:
if theHandler.im_self == inObject:
self -= theHandler