Zope Event

6.1 · active · verified Sun Mar 29

The `zope.event` package provides a very basic, synchronous event publishing system. It enables application code to generate events without requiring awareness of the specific frameworks that might handle them. The current version is 6.1, released on 2025-11-07. Zope ecosystem packages typically follow semantic versioning, with minor releases approximately every 2-6 months and major releases every 2-3 years.

Warnings

Install

Imports

Quickstart

To use `zope.event`, you define an event object (which can be any Python object), define callable functions or methods as subscribers that accept the event object, and then add these subscribers to the `zope.event.subscribers` list. The `zope.event.notify()` function is then called with the event object, which dispatches it to all registered subscribers. The quickstart code temporarily clears the global subscriber list for isolation, which is not typical in a running application but useful for isolated examples or testing.

import zope.event

class MyEvent:
    def __init__(self, data):
        self.data = data

def my_subscriber(event):
    print(f"Subscriber 1 received event with data: {event.data}")

def another_subscriber(event):
    print(f"Subscriber 2 processed event: {event.data.upper()}")

# Clear existing subscribers for example isolation (optional for real apps)
# You might not want to do this in production, especially if other parts
# of your application register global subscribers.
_old_subscribers = zope.event.subscribers[:]
del zope.event.subscribers[:]

# Register subscribers
zope.event.subscribers.append(my_subscriber)
zope.event.subscribers.append(another_subscriber)

# Create and notify an event
event = MyEvent("hello world")
zope.event.notify(event)

# Unsubscribe (optional)
zope.event.subscribers.remove(my_subscriber)
zope.event.subscribers.remove(another_subscriber)

# Restore original subscribers (if applicable)
zope.event.subscribers.extend(_old_subscribers)

# Example of notifying with a simple object (any object can be an event)
zope.event.notify(42)
zope.event.notify("a string event")

print("\nDemonstrating direct manipulation of subscribers (for testing/setup):")
# Direct manipulation of the subscribers list is often used in tests
# or initialization, but higher-level frameworks like zope.component
# provide more sophisticated subscription mechanisms.
print(f"Current subscribers: {zope.event.subscribers}")

view raw JSON →