Created
November 11, 2013 02:36
-
-
Save diegotoral/7406888 to your computer and use it in GitHub Desktop.
A simple signal class for Python.
This file contains hidden or 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 Signal(object): | |
| """ | |
| Signal class. | |
| """ | |
| def __init__(self, sender): | |
| super(Signal, self).__init__() | |
| self.sender = sender | |
| self.listeners = [] | |
| def connect(self, callback): | |
| """ | |
| Connect a callback with this signal. | |
| """ | |
| self.listeners.append(callback) | |
| def disconnect(self, callback): | |
| """ | |
| Disconnect the given callback from this signal. | |
| """ | |
| self.listeners.remove(callback) | |
| def fire(self, *args, **kwargs): | |
| """ | |
| Fire this signal. | |
| """ | |
| for listener in self.listeners: | |
| listener(sender=self.sender, *args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment