Skip to content

Observable: Basic API

Creating an Observable

from nuiitivet.observable import Observable

name = Observable("Alice")
age = Observable(20)
items = Observable([])

Getting and Setting Values

current = age.value
age.value = 21

Subscribing and Unsubscribing

subscription = age.subscribe(lambda value: print(value))
subscription.unsubscribe()

In most UI cases, cleanup is handled automatically by the framework lifecycle.

Custom Comparison Function

By default, value equality uses ==.

count = Observable(0)

You can customize comparison behavior when needed.

always_notify = Observable(0, compare=lambda a, b: False)

def compare_users(a, b):
    if a is None or b is None:
        return a is b
    return a.id == b.id

user = Observable(None, compare=compare_users)

Next Steps