Skip to content

Instantly share code, notes, and snippets.

@hinnerk
Created November 8, 2012 13:38
Show Gist options
  • Save hinnerk/4038848 to your computer and use it in GitHub Desktop.
Save hinnerk/4038848 to your computer and use it in GitHub Desktop.
Observer-Pattern in Python
class Event(object):
def __init__(self, **kwargs):
for k, v in kwargs.iteritems():
setattr(self, k, v)
class Observable(object):
def __init__(self):
self.callbacks = set()
def subscribe(self, callback):
self.callbacks.add(callback)
def unsubscribe(self, callback):
self.callbacks.discard(callback)
def fire(self, **attrs):
e = Event(source=self, **attrs)
for fn in self.callbacks:
fn(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment