Skip to content

Instantly share code, notes, and snippets.

@diegotoral
Created November 11, 2013 02:36
Show Gist options
  • Select an option

  • Save diegotoral/7406888 to your computer and use it in GitHub Desktop.

Select an option

Save diegotoral/7406888 to your computer and use it in GitHub Desktop.
A simple signal class for Python.
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