Skip to content

Instantly share code, notes, and snippets.

@gdugas
Created April 23, 2015 15:22
Show Gist options
  • Save gdugas/576a1cff338e15b1d6c8 to your computer and use it in GitHub Desktop.
Save gdugas/576a1cff338e15b1d6c8 to your computer and use it in GitHub Desktop.
Simple event dispatcher
class Event(object):
def __init__(self):
self.callables = {}
def on(self, func, uid=None):
if not callable(func):
raise TypeError("callable required")
if not uid:
uid = str(hash(func))
self.callables[uid] = func
def off(self, func):
if type(func) == str:
del self.callables[func]
else:
del self.callables[str(hash(func))]
def fire(self, *args, **kwargs):
for key in self.callables:
func = self.callables[key]
yield func(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment