Observable

raw JSON →
1.0.3 verified Mon Apr 27 auth: no python maintenance

Minimalist event system for Python, inspired by the Observable pattern. Current version 1.0.3. Low release cadence; last stable release in 2017.

pip install observable
error ImportError: cannot import name 'Observer' from 'observable'
cause There is no class named Observer; the correct class is Observable.
fix
Change import to: from observable import Observable
error TypeError: callback() takes 1 positional argument but 2 were given
cause The callback expects a single argument but trigger is called with two arguments (event name and data). The callback receives all arguments passed to trigger after the event name.
fix
Adjust callback signature to accept the correct number of arguments, e.g., def callback(event_name, data): or use *args.
breaking Observable is not a mixin; you must subclass it. Adding Observable as a base class to an existing class may cause metaclass conflicts.
fix Use class MyClass(Observable): instead of combining with other base classes unless using compatible metaclasses.
gotcha Events are identified by string names. Triggering an event with no listeners will silently do nothing. No error is raised.
fix Verify listeners are registered before triggering, or implement a check if needed.
gotcha Callback arguments are passed as positional arguments to trigger. Ensure your callback signature matches the number of arguments you pass; otherwise, a TypeError will occur.
fix Use *args and **kwargs in callback to handle any number of arguments, or strictly match signatures.
deprecated The library has not been updated since 2017. The API is stable but no longer maintained. Python 3.6+ compatibility may require testing.
fix Consider alternative libraries like 'pyee' or 'blinker' for active support.

Create a subclass of Observable, register a callback with .on(), and fire events with .trigger().

from observable import Observable

class MyObservable(Observable):
    pass

obj = MyObservable()

def callback(data):
    print(f"Event received: {data}")

obj.on('change', callback)
obj.trigger('change', 'Hello, world!')