Jason Hildebrand wrote:
> Several years ago I wrote a framework in PHP which provided a lot of
> callbacks for customization. On problem I ran into is that depending on
> how you need to customize, you might want to execute some code before,
> instead of, or after an event:
> This led to a proliferation of callback functions which achieved the goal of
> customizability, but had several shortcomings:
> - difficult to keep track of all the callback methods and their names
> - to maintain sanity, the three callbacks for an event (pre, the
> event itself, and post) need to have the same parameters, which
> means changing in three places
> - I ended up with a pile of 'bare' callback functions (not attached to a
> class), for which it would be difficult for someone else to understand
> the interplay.
> I eventually realized that using classes would clean a lot of this up.
> With classes you only need one method per event. Any subclass which
> overrides this method can call the superclass' method (or not) when it
> needs to, thereby controlling whether the new functionality is executed
> before, instead of, or after the event.
Well, since we're talking about events, maybe it should done as events.
The problem I see with classes is that they aren't composable -- you
can make one, and only one subclass (at least, only one subclass will be
used). Mix-ins are composable, since you can apply the mix-in to an
already-mixed-in class (well, at least with your patch), but they are a
little difficult to work with, and tend to be tightly coupled to the
code they are modifying.
Events don't really solve those problems, but at least they make it a
bit more explicit. You still have to deal with the order in which
registered actions are executed, what effect those actions can have on
the flow of the code (e.g., can they stop the event, or modify the
event), and you may want both pre- and post-events for nearly any event.
And you have to specify all the events, and any data associated with
But I think events are a good metaphor, even if it's still work. I
believe there's some good generic frameworks on PyPI for event
dispatching. At least, I remember seeing one somewhere that looked
good, in the last month or two.