Created
November 8, 2012 13:38
-
-
Save hinnerk/4038848 to your computer and use it in GitHub Desktop.
Observer-Pattern in Python
This file contains 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 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