Using zope.event

At its core, zope.event simply consists of two things: a list of subscribers (callable objects), and an API function (notify()) that invokes those subscribers in order.

Notifications

Alerting subscribers that an event has occurred is referred to as “notifying them”, or sometimes “sending them the event.”

The package provides a notify() function, which is used to notify subscribers that something has happened:

>>> class MyEvent(object):
...     pass

>>> event = MyEvent()
>>> zope.event.notify(event)

The notify function is called with a single object, which we call an event. Any object will do:

>>> zope.event.notify(None)
>>> zope.event.notify(42)

Our subscriber list is currently empty, so nothing happened in response to these notifications.

Subscribers

A subscriber is a callable object that takes one argument, an object that we call the event.

Application code can manage subscriptions by manipulating the list that zope.event maintains. This list starts out empty.

>>> import zope.event
>>> zope.event.subscribers
[]

Note

Users of higher-level event frameworks will not typically need to modify this subscriber list directly. Generally, such event (or application) frameworks will provide more sophisticated subscription mechanisms that build on this simple mechanism. The frameworks will install subscribers that then distribute the event to other subscribers based on event types or data.

A simple framework that is based on the class hierarchy is distributed with this package and described in Class-based event handlers.

A higher-level event framework is distributed with zope.component. For information on using zope.event together with zope.component, see zope.component’s documentation.

Trivial Subscribers

As mentioned above, subscribers are simply callable objects that are added to the subscriptions list:

>>> def f(event):
...     print 'got:', event
>>> zope.event.subscribers.append(f)

>>> zope.event.notify(42)
got: 42

>>> def g(event):
...     print 'also got:', event

>>> zope.event.subscribers.append(g)

>>> zope.event.notify(42)
got: 42
also got: 42

To unsubscribe, simply remove a subscriber from the list:

>>> zope.event.subscribers.remove(f)
>>> zope.event.notify(42)
also got: 42

>>> zope.event.subscribers.remove(g)
>>> zope.event.notify(42)