patternpythonMinor
Python Event Dispatcher
Viewed 0 times
pythondispatcherevent
Problem
This code creates a singleton event dispatcher. It's called like this:
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.
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)Solution
Checking items in dictionnary
There is no need to call
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:
trigger
Since
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
Proposed alternative
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)Code Snippets
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)Context
StackExchange Code Review Q#109605, answer score: 7
Revisions (0)
No revisions yet.