Skip to content

Instantly share code, notes, and snippets.

@marc-x-andre
Created June 26, 2020 21:23
Show Gist options
  • Save marc-x-andre/1c55b3fafd1d00cfdaa205ec53a08cf3 to your computer and use it in GitHub Desktop.
Save marc-x-andre/1c55b3fafd1d00cfdaa205ec53a08cf3 to your computer and use it in GitHub Desktop.
A event emitter in Python
from typing import Dict
class EventEmitter:
def __init__(self):
self._callbacks: Dict[str, callable] = {}
def on(self, event_name, function):
self._callbacks[event_name] = self._callbacks.get(event_name, []) + [function]
return function
def emit(self, event_name, *args, **kwargs):
[function(*args, **kwargs) for function in self._callbacks.get(event_name, [])]
def off(self, event_name, function):
self._callbacks.get(event_name, []).remove(function)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment