The concept of events is heavily used in GUI libraries and is the foundation
for most implementations of the MVC (Model, View, Controller) design pattern.
Another prominent use of events is in communication protocol stacks, where
lower protocol layers need to inform upper layers of incoming data and the
like. Here is a handy class that encapsulates the core to event subscription
and event firing and feels like a “natural” part of the language.

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. To subscribe to an event:

Note that by default Events does not check if an event that is
being subscribed to can actually be fired, unless the class attribute
__events__ is defined. This can cause a problem if an event name is
slightly misspelled. If this is an issue, subclass Events and
list the possible events, like:

classMyEvents(Events):__events__=('on_this','on_that',)events=MyEvents()# this will raise an EventsException as `on_change` is unknown to MyEvents:events.on_change+=changed

You can also predefine events for a single Events instance by
passing an iterator to the constructor.

events=Events(('on_this','on_that'))# this will raise an EventsException as `on_change` is unknown to MyEvents:events.on_change+=changed

It is recommended to use the constructor method for one time use cases. For more
complicated use cases, it is recommended to subclass Events
and define __events__.

You can also leverage both the constructor method and the __events__
attribute to restrict events for specific instances:

DatabaseEvents(Events):__events__=('insert','update','delete','select')audit_events=('select')AppDatabaseEvents=DatabaseEvents()# only knows the 'select' event from DatabaseEventsAuditDatabaseEvents=DatabaseEvents(audit_events)