Created
October 14, 2013 02:11
-
-
Save goliatone/6969679 to your computer and use it in GitHub Desktop.
Simple and dirty EventDispatcher implementation in Python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EventDispatcher(object): | |
def __init__(self): | |
self._events = {} | |
def on(self, type, handler): | |
handlers = self._events.setdefault(type, []) | |
handlers.append(handler) | |
def emit(self, type, *args, **kwargs): | |
try: | |
for handler in self._events.get(type): | |
handler(*args, **kwargs) | |
except: | |
pass | |
def off(self, type, handler): | |
try: | |
self._events.get(type).remove(handler) | |
except: | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment