Events (pyeve)

0.5 · maintenance · verified Sat Mar 28

The 'events' library for Python, currently at version 0.5, aims to bring the elegance of C# EventHandler to Python. It provides a simple, lightweight mechanism for event subscription and firing, encapsulating the core functionality for attaching and invoking callback functions (event handlers) in sequence, thus facilitating event-driven programming patterns in a straightforward manner. The library is considered mature and stable, with its last release in July 2023, and sees infrequent updates.

Warnings

Install

Imports

Quickstart

Demonstrates how to create an `Events` object, subscribe multiple callback functions using `+=`, fire events, and unsubscribe callbacks using `-=`.

from events import Events

def my_event_handler(reason, data=None):
    print(f"Event fired: {reason}")
    if data:
        print(f"  Data: {data}")

# Create an Events instance
app_events = Events()

# Subscribe a handler to an event named 'on_change'
app_events.on_change += my_event_handler

# Fire the 'on_change' event
print("Firing event: 'initial change'")
app_events.on_change("initial change", data={"user": "admin"})

# Subscribe another handler to the same event
def another_handler(reason):
    print(f"Another handler received: {reason}")
app_events.on_change += another_handler

# Fire the event again
print("\nFiring event again: 'second change'")
app_events.on_change("second change")

# Unsubscribe a handler
app_events.on_change -= my_event_handler

# Fire the event one last time (only 'another_handler' will be invoked)
print("\nFiring event after unsubscription: 'final change'")
app_events.on_change("final change")

view raw JSON →