So last week I got an email from my friend Michael Kennedy. Michael runs the TalkPython Training website, arguably the best place where you can learn Python today. He also hosts two popular Python podcasts: TalkPython and PythonBytes, the latter co-hosted with Brian Okken. He is super active in the Python space, so much that he received the Python Software Foundation Fellow Membership back in 2018. I first met Michael back in 2014, I think, at MongoDB Headquarters in New York, where we were both invited as part of the MongoDB Masters program. We shared a C#/.NET background, which was something of note in that context, and I remember Michael being curious about me embracing Python and my first open-source release in that space. Fun times.
Anyways, back to his email. He mentioned my little Events library and told me that they covered it in episode #194 of the PythonBytes podcast. I was delighted, of course, and a little surprised too, as Events has been out for several years. Like its bigger brother Cerberus, I built Events as I was working on the Eve project. Unlike Cerberus, which got a lot of traction on its own (outside of the Eve eco-system), Events did not get a lot of attention. Not as a stand-alone at least. People are leveraging its features every time they use event hooks in Eve, but, albeit mentioned in the documentation, few realize that they can add support for dynamic callbacks directly to their projects.
Michael mentioning Events in the podcast was gratifying. He also published a gist with a concrete usage example, and then he went ahead and submitted a pull request with some documentation improvements, with some more PRs to come in the future.
But what exactly is Events? The C# language provides a handy way to declare, subscribe to, and fire events. Technically, an event is a “slot” where callback functions (event handlers) can be attached to - a process referred to as “subscribing to an event.” Events is a little handy package that encapsulates the core to C# event subscription and firing, to make it feel like a natural part of the Python language. Not surprisingly, Events is dubbed Python Event Handling, the C# Style.
>>> def something_changed(reason): ... print "something changed because %s" % reason ... >>> from events import Events >>> events = Events() >>> events.on_change += something_changed
Multiple callback functions can subscribe to the same event. When the event fires, all attached event handlers are invoked in sequence. To fire the event, perform a call on the slot:
>>> events.on_change('it had to happen') 'something changed because it had to happen'
Intrigued? I sure hope so. Check out the Events repository on GitHub.