Python Event Dispatcher

This code creates a singleton event dispatcher. It's called like this:

eventDispatcher = Event()
eventDispatcher.on("event", handler)
eventDispatcher.trigger("event", 5)


I'm wondering if this is "the python way," or if it could be improved? Especially the class, I'm not sure about creating a new object for every use of the dispatcher.

class Event():
events = {}

def on(self, event, func):
if event not in self.events.keys():
self.events[event] = []

self.events[event].append(func)

def trigger(self, event, *args, **namedArgs):
if event in self.events.keys():
for func in self.events[event]:
func(*args, **namedArgs)


Checking items in dictionnary

There is no need to call keys when checking for dictionnary membership. Much like there is no need of True in if my_bool == True.

on

Checking if a key exist and set it to a default value otherwise is so common that you have 2 standard ways of doing it: dict.setdefault(...) or using a collections.defaultdict.

trigger

Since KeyError should normaly not happen, you can use EAFP approach by using try...except instead of if. It's a tiny bit faster if exceptions are quite rare. Moreover, by using defaultdict you are guaranteed that every access to an item will be valid, thus eliminating the need to check membership of any key.

class

Since the only state you need is the one of the class, and in order to avoid instanciating to call methods, you can use @staticmethods and call them with Event.on(...) and Event.trigger(..).

Proposed alternative

from collections import defaultdict

class Event():
__events = defaultdict(list)

@staticmethod
def on(event, func):
Event.__events[event].append(func)

@staticmethod
def trigger(event, *args, **namedArgs):
for func in Event.__events[event]:
func(*args, **namedArgs)

• Is there a reason two underscores are used with the events variable? (Rather than just one.) – jayshua Nov 3 '15 at 13:47
• @jayshua It is to trigger name mangling to better emphasize that anything related to __events is Event's business. You can use one or no underscore if you see fit. – Mathias Ettinger Nov 3 '15 at 14:21

Very minor note, but the normal Python names are args and kwargs (ie. Keyword Arguments).

def trigger(self, event, *args, **kwargs):

• This should be a comment to the previous answer—not posted as its own answer. – Zearin Nov 25 '15 at 16:05
• @Zearin It's not related to the other answer, it's a separate note. – SuperBiasedMan Nov 25 '15 at 16:08