Skip to content

Instantly share code, notes, and snippets.

@goliatone
Created October 14, 2013 02:11
Show Gist options
  • Save goliatone/6969679 to your computer and use it in GitHub Desktop.
Save goliatone/6969679 to your computer and use it in GitHub Desktop.
Simple and dirty EventDispatcher implementation in Python.
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