Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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)
share|improve this question

2 Answers 2

up vote 6 down vote accepted

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)
share|improve this answer
    
Is there a reason two underscores are used with the events variable? (Rather than just one.) – jayshua 16 hours ago
    
@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 15 hours ago

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

def trigger(self, event, *args, **kwargs):
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.