Core¶
signified.core
¶
Core reactive programming functionality.
__all__
module-attribute
¶
__all__ = ['Observer', 'ReactiveMixIn', 'Variable', 'Signal', 'Computed', 'computed', 'unref', 'has_value', 'deep_unref', 'reactive_method', 'as_signal']
ReactiveMixIn
¶
Methods for easily creating reactive values.
__getattr__
¶
__getattr__(name: str) -> Computed[Any]
Create a reactive value for retrieving an attribute from self.value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the attribute to access. |
required |
Returns:
| Type | Description |
|---|---|
Union[T, Computed[Any]]
|
A reactive value for the attribute access. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the attribute doesn't exist. |
Note
Type inference is poor whenever __getattr__ is used.
__call__
¶
Create a reactive value for calling self.value(*args, **kwargs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
P.args
|
Positional arguments to pass to the callable value. |
()
|
**kwargs
|
P.kwargs
|
Keyword arguments to pass to the callable value. |
{}
|
Returns:
| Type | Description |
|---|---|
Computed[R]
|
A reactive value for the function call. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the value is not callable. |
__abs__
¶
as_bool
¶
Return a reactive value for the boolean value of self.
Note
__bool__ cannot be implemented to return a non-bool, so it is provided as a method.
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__str__
¶
Return a string of the current value.
Note
This is not reactive.
Returns:
| Type | Description |
|---|---|
str
|
A string representation of |
__round__
¶
__round__() -> Computed[int]
__round__(ndigits: None) -> Computed[int]
__round__(ndigits: int) -> Computed[int]
__round__() -> Computed[int]
__round__(ndigits: None) -> Computed[int]
__round__(ndigits: int) -> Computed[int]
__round__() -> Computed[int]
__round__(ndigits: None) -> Computed[int]
__round__(ndigits: int) -> Computed[float]
__ceil__
¶
Return a reactive value for the ceiling of self.
Returns:
| Type | Description |
|---|---|
Computed[int]
|
A reactive value for |
__floor__
¶
Return a reactive value for the floor of self.
Returns:
| Type | Description |
|---|---|
Computed[int]
|
A reactive value for |
__invert__
¶
__neg__
¶
__pos__
¶
__trunc__
¶
__add__
¶
__add__(other: HasValue[int] | HasValue[float]) -> Computed[float]
__add__(other: HasValue[float]) -> Computed[float]
__add__(other: HasValue[Y]) -> Computed[R]
__add__(other: Any) -> Computed[Any]
Return a reactive value for the sum of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to add. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__and__
¶
Return a reactive value for the bitwise AND of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to AND with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
contains
¶
Return a reactive value for whether other is in self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to check for containment. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__divmod__
¶
__divmod__(other: HasValue[int]) -> Computed[tuple[int, int]]
__divmod__(other: HasValue[bool] | HasValue[int]) -> Computed[tuple[int, int]]
__divmod__(other: Any) -> Computed[tuple[float, float]]
is_not
¶
Return a reactive value for whether self is not other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value is not other. |
eq
¶
Return a reactive value for whether self equals other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value == other. |
Note
We can't overload __eq__ because it interferes with basic Python operations.
__floordiv__
¶
__floordiv__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__floordiv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the floor division of self by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for self.value // other.value. |
__ge__
¶
Return a reactive value for whether self is greater than or equal to other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value >= other. |
__gt__
¶
Return a reactive value for whether self is greater than other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value > other. |
__le__
¶
Return a reactive value for whether self is less than or equal to other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__lt__
¶
Return a reactive value for whether self is less than other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__lshift__
¶
Return a reactive value for self left-shifted by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The number of positions to shift. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__matmul__
¶
Return a reactive value for the matrix multiplication of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to multiply with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__mod__
¶
__mod__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__mod__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self modulo other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__mul__
¶
__mul__(other: HasValue[int]) -> Computed[str]
__mul__(other: HasValue[int]) -> Computed[list[V]]
__mul__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the product of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to multiply with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__ne__
¶
Return a reactive value for whether self is not equal to other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__or__
¶
Return a reactive value for the bitwise OR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to OR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rshift__
¶
Return a reactive value for self right-shifted by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The number of positions to shift. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__pow__
¶
__pow__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__pow__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self raised to the power of other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The exponent. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__sub__
¶
Return a reactive value for the difference of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to subtract. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__truediv__
¶
__truediv__(other: HasValue[int]) -> Computed[float]
__truediv__(other: HasValue[float]) -> Computed[float]
__truediv__(other: HasValue[int]) -> Computed[float]
__truediv__(other: HasValue[float]) -> Computed[float]
__truediv__(other: HasValue[bool] | HasValue[int] | HasValue[float]) -> Computed[float]
__truediv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self divided by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__xor__
¶
Return a reactive value for the bitwise XOR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to XOR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__radd__
¶
__radd__(other: HasValue[int] | HasValue[float]) -> Computed[float]
__radd__(other: HasValue[_SupportsAdd[T, R]]) -> Computed[R]
__radd__(other: Any) -> Computed[Any]
Return a reactive value for the sum of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to add. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rand__
¶
Return a reactive value for the bitwise AND of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to AND with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rdivmod__
¶
__rdivmod__(other: HasValue[int]) -> Computed[tuple[int, int]]
__rdivmod__(other: HasValue[int]) -> Computed[tuple[int, int]]
__rdivmod__(other: HasValue[bool]) -> Computed[tuple[int, int]]
__rdivmod__(other: Any) -> Computed[tuple[float, float]]
__rfloordiv__
¶
__rfloordiv__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__rfloordiv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the floor division of other by self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the numerator. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rmod__
¶
__rmod__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__rmod__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for other modulo self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The dividend. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rmul__
¶
__rmul__(other: HasValue[int]) -> Computed[str]
__rmul__(other: HasValue[int]) -> Computed[list[V]]
__rmul__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the product of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to multiply with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__ror__
¶
Return a reactive value for the bitwise OR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to OR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rpow__
¶
__rpow__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__rpow__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self raised to the power of other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The base. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rsub__
¶
Return a reactive value for the difference of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to subtract from. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rtruediv__
¶
__rtruediv__(other: HasValue[int]) -> Computed[float]
__rtruediv__(other: HasValue[float]) -> Computed[float]
__rtruediv__(other: HasValue[int]) -> Computed[float]
__rtruediv__(other: HasValue[float]) -> Computed[float]
__rtruediv__(other: HasValue[bool] | HasValue[int] | HasValue[float]) -> Computed[float]
__rtruediv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self divided by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rxor__
¶
Return a reactive value for the bitwise XOR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to XOR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__getitem__
¶
__getitem__(key: slice) -> Computed[list[V]]
__getitem__(key: slice) -> Computed[tuple[V, ...]]
__getitem__(key: slice) -> Computed[str]
__getitem__(key: HasValue[SupportsIndex] | HasValue[int]) -> Computed[V]
__getitem__(key: HasValue[SupportsIndex] | HasValue[int]) -> Computed[V]
__getitem__(key: HasValue[SupportsIndex] | HasValue[int]) -> Computed[str]
__getitem__(key: HasValue[K]) -> Computed[V]
__getitem__(key: HasValue[K]) -> Computed[V]
__getitem__(key: Any) -> Computed[Any]
Return a reactive value for the item or slice of self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The index or slice to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__setattr__
¶
Set an attribute on the underlying self.value.
Note
It is necessary to set the attribute via the Signal, rather than the
underlying signal.value, to properly notify downstream observers
of changes. Reason being, mutable objects that, for example, fallback
to id comparison for equality checks will appear as if nothing changed
even if one of its attributes changed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the attribute to access. |
required |
value
|
Any
|
The value to set it to. |
required |
Example
>>> class Person:
... def __init__(self, name: str):
... self.name = name
... def greet(self) -> str:
... return f"Hi, I'm {self.name}!"
>>> s = Signal(Person("Alice"))
>>> result = s.greet()
>>> result.value
"Hi, I'm Alice!"
>>> s.name = "Bob" # Modify attribute on Person instance through the reactive value s
>>> result.value
"Hi, I'm Bob!"
__setitem__
¶
Set an item on the underlying self.value.
Note
It is necessary to set the item via the Signal, rather than the
underlying signal.value, to properly notify downstream observers
of changes. Reason being, mutable objects that, for example, fallback
to id comparison for equality checks will appear as if nothing changed
even an element of the object is changed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The key to change. |
required |
value
|
Any
|
The value to set it to. |
required |
Example
```py
s = Signal([1, 2, 3]) result = computed(sum)(s) result.value 6 s[1] = 4 result.value 8
where
¶
Return a reactive value for a if self is True, else b.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
HasValue[A]
|
The value to return if |
required |
b
|
HasValue[B]
|
The value to return if |
required |
Returns:
| Type | Description |
|---|---|
Computed[A | B]
|
A reactive value for |
Variable
¶
Bases: ABC, ReactiveMixIn[T]
An abstract base class for reactive values.
A reactive value is an object that can be observed by observer for changes and can notify observers when its value changes. This class implements both the observer and observable patterns.
This class implements both the observer and observable pattern.
Subclasses should implement the update method.
Attributes:
| Name | Type | Description |
|---|---|---|
_observers |
list[Observer]
|
List of observers subscribed to this variable. |
subscribe
¶
Subscribe an observer to this variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observer
|
Observer
|
The observer to subscribe. |
required |
unsubscribe
¶
Unsubscribe an observer from this variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observer
|
Observer
|
The observer to unsubscribe. |
required |
observe
¶
Subscribe the observer (self) to all items that are Observable.
This method handles arbitrarily nested iterables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Any
|
A single item, an iterable, or a nested structure of items to potentially subscribe to. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
self |
unobserve
¶
Unsubscribe the observer (self) from all items that are Observable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Any
|
A single item or an iterable of items to potentially unsubscribe from. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
self |
update
abstractmethod
¶
Update method to be overridden by subclasses.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If not overridden by a subclass. |
__format__
¶
Format the variable with custom display options.
Format options: :n - just the name (or type+id if unnamed) :d - full debug info empty - just the value in brackets (default)
__getattr__
¶
__getattr__(name: str) -> Computed[Any]
Create a reactive value for retrieving an attribute from self.value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the attribute to access. |
required |
Returns:
| Type | Description |
|---|---|
Union[T, Computed[Any]]
|
A reactive value for the attribute access. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the attribute doesn't exist. |
Note
Type inference is poor whenever __getattr__ is used.
__call__
¶
Create a reactive value for calling self.value(*args, **kwargs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
P.args
|
Positional arguments to pass to the callable value. |
()
|
**kwargs
|
P.kwargs
|
Keyword arguments to pass to the callable value. |
{}
|
Returns:
| Type | Description |
|---|---|
Computed[R]
|
A reactive value for the function call. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the value is not callable. |
__abs__
¶
as_bool
¶
Return a reactive value for the boolean value of self.
Note
__bool__ cannot be implemented to return a non-bool, so it is provided as a method.
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__str__
¶
Return a string of the current value.
Note
This is not reactive.
Returns:
| Type | Description |
|---|---|
str
|
A string representation of |
__round__
¶
__round__() -> Computed[int]
__round__(ndigits: None) -> Computed[int]
__round__(ndigits: int) -> Computed[int]
__round__() -> Computed[int]
__round__(ndigits: None) -> Computed[int]
__round__(ndigits: int) -> Computed[int]
__round__() -> Computed[int]
__round__(ndigits: None) -> Computed[int]
__round__(ndigits: int) -> Computed[float]
__ceil__
¶
Return a reactive value for the ceiling of self.
Returns:
| Type | Description |
|---|---|
Computed[int]
|
A reactive value for |
__floor__
¶
Return a reactive value for the floor of self.
Returns:
| Type | Description |
|---|---|
Computed[int]
|
A reactive value for |
__invert__
¶
__neg__
¶
__pos__
¶
__trunc__
¶
__add__
¶
__add__(other: HasValue[int] | HasValue[float]) -> Computed[float]
__add__(other: HasValue[float]) -> Computed[float]
__add__(other: HasValue[Y]) -> Computed[R]
__add__(other: Any) -> Computed[Any]
Return a reactive value for the sum of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to add. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__and__
¶
Return a reactive value for the bitwise AND of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to AND with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
contains
¶
Return a reactive value for whether other is in self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to check for containment. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__divmod__
¶
__divmod__(other: HasValue[int]) -> Computed[tuple[int, int]]
__divmod__(other: HasValue[bool] | HasValue[int]) -> Computed[tuple[int, int]]
__divmod__(other: Any) -> Computed[tuple[float, float]]
is_not
¶
Return a reactive value for whether self is not other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value is not other. |
eq
¶
Return a reactive value for whether self equals other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value == other. |
Note
We can't overload __eq__ because it interferes with basic Python operations.
__floordiv__
¶
__floordiv__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__floordiv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the floor division of self by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for self.value // other.value. |
__ge__
¶
Return a reactive value for whether self is greater than or equal to other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value >= other. |
__gt__
¶
Return a reactive value for whether self is greater than other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value > other. |
__le__
¶
Return a reactive value for whether self is less than or equal to other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__lt__
¶
Return a reactive value for whether self is less than other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__lshift__
¶
Return a reactive value for self left-shifted by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The number of positions to shift. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__matmul__
¶
Return a reactive value for the matrix multiplication of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to multiply with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__mod__
¶
__mod__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__mod__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self modulo other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__mul__
¶
__mul__(other: HasValue[int]) -> Computed[str]
__mul__(other: HasValue[int]) -> Computed[list[V]]
__mul__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the product of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to multiply with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__ne__
¶
Return a reactive value for whether self is not equal to other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__or__
¶
Return a reactive value for the bitwise OR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to OR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rshift__
¶
Return a reactive value for self right-shifted by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The number of positions to shift. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__pow__
¶
__pow__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__pow__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self raised to the power of other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The exponent. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__sub__
¶
Return a reactive value for the difference of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to subtract. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__truediv__
¶
__truediv__(other: HasValue[int]) -> Computed[float]
__truediv__(other: HasValue[float]) -> Computed[float]
__truediv__(other: HasValue[int]) -> Computed[float]
__truediv__(other: HasValue[float]) -> Computed[float]
__truediv__(other: HasValue[bool] | HasValue[int] | HasValue[float]) -> Computed[float]
__truediv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self divided by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__xor__
¶
Return a reactive value for the bitwise XOR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to XOR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__radd__
¶
__radd__(other: HasValue[int] | HasValue[float]) -> Computed[float]
__radd__(other: HasValue[_SupportsAdd[T, R]]) -> Computed[R]
__radd__(other: Any) -> Computed[Any]
Return a reactive value for the sum of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to add. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rand__
¶
Return a reactive value for the bitwise AND of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to AND with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rdivmod__
¶
__rdivmod__(other: HasValue[int]) -> Computed[tuple[int, int]]
__rdivmod__(other: HasValue[int]) -> Computed[tuple[int, int]]
__rdivmod__(other: HasValue[bool]) -> Computed[tuple[int, int]]
__rdivmod__(other: Any) -> Computed[tuple[float, float]]
__rfloordiv__
¶
__rfloordiv__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__rfloordiv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the floor division of other by self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the numerator. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rmod__
¶
__rmod__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__rmod__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for other modulo self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The dividend. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rmul__
¶
__rmul__(other: HasValue[int]) -> Computed[str]
__rmul__(other: HasValue[int]) -> Computed[list[V]]
__rmul__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the product of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to multiply with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__ror__
¶
Return a reactive value for the bitwise OR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to OR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rpow__
¶
__rpow__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__rpow__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self raised to the power of other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The base. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rsub__
¶
Return a reactive value for the difference of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to subtract from. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rtruediv__
¶
__rtruediv__(other: HasValue[int]) -> Computed[float]
__rtruediv__(other: HasValue[float]) -> Computed[float]
__rtruediv__(other: HasValue[int]) -> Computed[float]
__rtruediv__(other: HasValue[float]) -> Computed[float]
__rtruediv__(other: HasValue[bool] | HasValue[int] | HasValue[float]) -> Computed[float]
__rtruediv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self divided by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rxor__
¶
Return a reactive value for the bitwise XOR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to XOR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__getitem__
¶
__getitem__(key: slice) -> Computed[list[V]]
__getitem__(key: slice) -> Computed[tuple[V, ...]]
__getitem__(key: slice) -> Computed[str]
__getitem__(key: HasValue[SupportsIndex] | HasValue[int]) -> Computed[V]
__getitem__(key: HasValue[SupportsIndex] | HasValue[int]) -> Computed[V]
__getitem__(key: HasValue[SupportsIndex] | HasValue[int]) -> Computed[str]
__getitem__(key: HasValue[K]) -> Computed[V]
__getitem__(key: HasValue[K]) -> Computed[V]
__getitem__(key: Any) -> Computed[Any]
Return a reactive value for the item or slice of self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The index or slice to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__setattr__
¶
Set an attribute on the underlying self.value.
Note
It is necessary to set the attribute via the Signal, rather than the
underlying signal.value, to properly notify downstream observers
of changes. Reason being, mutable objects that, for example, fallback
to id comparison for equality checks will appear as if nothing changed
even if one of its attributes changed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the attribute to access. |
required |
value
|
Any
|
The value to set it to. |
required |
Example
>>> class Person:
... def __init__(self, name: str):
... self.name = name
... def greet(self) -> str:
... return f"Hi, I'm {self.name}!"
>>> s = Signal(Person("Alice"))
>>> result = s.greet()
>>> result.value
"Hi, I'm Alice!"
>>> s.name = "Bob" # Modify attribute on Person instance through the reactive value s
>>> result.value
"Hi, I'm Bob!"
__setitem__
¶
Set an item on the underlying self.value.
Note
It is necessary to set the item via the Signal, rather than the
underlying signal.value, to properly notify downstream observers
of changes. Reason being, mutable objects that, for example, fallback
to id comparison for equality checks will appear as if nothing changed
even an element of the object is changed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The key to change. |
required |
value
|
Any
|
The value to set it to. |
required |
Example
```py
s = Signal([1, 2, 3]) result = computed(sum)(s) result.value 6 s[1] = 4 result.value 8
where
¶
Return a reactive value for a if self is True, else b.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
HasValue[A]
|
The value to return if |
required |
b
|
HasValue[B]
|
The value to return if |
required |
Returns:
| Type | Description |
|---|---|
Computed[A | B]
|
A reactive value for |
Signal
¶
Bases: Variable[T]
Mutable source-of-truth reactive value.
Signal stores a value and notifies subscribers when that value changes.
It is typically used for application state that should be observed by
derived :class:Computed values.
The value property is read/write:
- reading value returns the resolved plain value
- assigning value updates dependencies and notifies observers when the
value changed
Signals can also proxy mutation operations (for example __setattr__ and
__setitem__) so in-place updates on wrapped objects can still trigger
reactivity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
HasValue[T]
|
Initial value to wrap. May be plain or reactive. |
required |
Example
__getattr__
¶
__getattr__(name: str) -> Computed[Any]
Create a reactive value for retrieving an attribute from self.value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the attribute to access. |
required |
Returns:
| Type | Description |
|---|---|
Union[T, Computed[Any]]
|
A reactive value for the attribute access. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the attribute doesn't exist. |
Note
Type inference is poor whenever __getattr__ is used.
__call__
¶
Create a reactive value for calling self.value(*args, **kwargs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
P.args
|
Positional arguments to pass to the callable value. |
()
|
**kwargs
|
P.kwargs
|
Keyword arguments to pass to the callable value. |
{}
|
Returns:
| Type | Description |
|---|---|
Computed[R]
|
A reactive value for the function call. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the value is not callable. |
__abs__
¶
as_bool
¶
Return a reactive value for the boolean value of self.
Note
__bool__ cannot be implemented to return a non-bool, so it is provided as a method.
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__str__
¶
Return a string of the current value.
Note
This is not reactive.
Returns:
| Type | Description |
|---|---|
str
|
A string representation of |
__round__
¶
__round__() -> Computed[int]
__round__(ndigits: None) -> Computed[int]
__round__(ndigits: int) -> Computed[int]
__round__() -> Computed[int]
__round__(ndigits: None) -> Computed[int]
__round__(ndigits: int) -> Computed[int]
__round__() -> Computed[int]
__round__(ndigits: None) -> Computed[int]
__round__(ndigits: int) -> Computed[float]
__ceil__
¶
Return a reactive value for the ceiling of self.
Returns:
| Type | Description |
|---|---|
Computed[int]
|
A reactive value for |
__floor__
¶
Return a reactive value for the floor of self.
Returns:
| Type | Description |
|---|---|
Computed[int]
|
A reactive value for |
__invert__
¶
__neg__
¶
__pos__
¶
__trunc__
¶
__add__
¶
__add__(other: HasValue[int] | HasValue[float]) -> Computed[float]
__add__(other: HasValue[float]) -> Computed[float]
__add__(other: HasValue[Y]) -> Computed[R]
__add__(other: Any) -> Computed[Any]
Return a reactive value for the sum of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to add. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__and__
¶
Return a reactive value for the bitwise AND of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to AND with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
contains
¶
Return a reactive value for whether other is in self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to check for containment. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__divmod__
¶
__divmod__(other: HasValue[int]) -> Computed[tuple[int, int]]
__divmod__(other: HasValue[bool] | HasValue[int]) -> Computed[tuple[int, int]]
__divmod__(other: Any) -> Computed[tuple[float, float]]
is_not
¶
Return a reactive value for whether self is not other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value is not other. |
eq
¶
Return a reactive value for whether self equals other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value == other. |
Note
We can't overload __eq__ because it interferes with basic Python operations.
__floordiv__
¶
__floordiv__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__floordiv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the floor division of self by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for self.value // other.value. |
__ge__
¶
Return a reactive value for whether self is greater than or equal to other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value >= other. |
__gt__
¶
Return a reactive value for whether self is greater than other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value > other. |
__le__
¶
Return a reactive value for whether self is less than or equal to other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__lt__
¶
Return a reactive value for whether self is less than other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__lshift__
¶
Return a reactive value for self left-shifted by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The number of positions to shift. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__matmul__
¶
Return a reactive value for the matrix multiplication of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to multiply with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__mod__
¶
__mod__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__mod__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self modulo other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__mul__
¶
__mul__(other: HasValue[int]) -> Computed[str]
__mul__(other: HasValue[int]) -> Computed[list[V]]
__mul__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the product of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to multiply with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__ne__
¶
Return a reactive value for whether self is not equal to other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__or__
¶
Return a reactive value for the bitwise OR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to OR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rshift__
¶
Return a reactive value for self right-shifted by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The number of positions to shift. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__pow__
¶
__pow__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__pow__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self raised to the power of other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The exponent. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__sub__
¶
Return a reactive value for the difference of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to subtract. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__truediv__
¶
__truediv__(other: HasValue[int]) -> Computed[float]
__truediv__(other: HasValue[float]) -> Computed[float]
__truediv__(other: HasValue[int]) -> Computed[float]
__truediv__(other: HasValue[float]) -> Computed[float]
__truediv__(other: HasValue[bool] | HasValue[int] | HasValue[float]) -> Computed[float]
__truediv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self divided by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__xor__
¶
Return a reactive value for the bitwise XOR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to XOR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__radd__
¶
__radd__(other: HasValue[int] | HasValue[float]) -> Computed[float]
__radd__(other: HasValue[_SupportsAdd[T, R]]) -> Computed[R]
__radd__(other: Any) -> Computed[Any]
Return a reactive value for the sum of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to add. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rand__
¶
Return a reactive value for the bitwise AND of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to AND with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rdivmod__
¶
__rdivmod__(other: HasValue[int]) -> Computed[tuple[int, int]]
__rdivmod__(other: HasValue[int]) -> Computed[tuple[int, int]]
__rdivmod__(other: HasValue[bool]) -> Computed[tuple[int, int]]
__rdivmod__(other: Any) -> Computed[tuple[float, float]]
__rfloordiv__
¶
__rfloordiv__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__rfloordiv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the floor division of other by self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the numerator. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rmod__
¶
__rmod__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__rmod__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for other modulo self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The dividend. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rmul__
¶
__rmul__(other: HasValue[int]) -> Computed[str]
__rmul__(other: HasValue[int]) -> Computed[list[V]]
__rmul__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the product of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to multiply with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__ror__
¶
Return a reactive value for the bitwise OR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to OR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rpow__
¶
__rpow__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__rpow__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self raised to the power of other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The base. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rsub__
¶
Return a reactive value for the difference of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to subtract from. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rtruediv__
¶
__rtruediv__(other: HasValue[int]) -> Computed[float]
__rtruediv__(other: HasValue[float]) -> Computed[float]
__rtruediv__(other: HasValue[int]) -> Computed[float]
__rtruediv__(other: HasValue[float]) -> Computed[float]
__rtruediv__(other: HasValue[bool] | HasValue[int] | HasValue[float]) -> Computed[float]
__rtruediv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self divided by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rxor__
¶
Return a reactive value for the bitwise XOR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to XOR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__getitem__
¶
__getitem__(key: slice) -> Computed[list[V]]
__getitem__(key: slice) -> Computed[tuple[V, ...]]
__getitem__(key: slice) -> Computed[str]
__getitem__(key: HasValue[SupportsIndex] | HasValue[int]) -> Computed[V]
__getitem__(key: HasValue[SupportsIndex] | HasValue[int]) -> Computed[V]
__getitem__(key: HasValue[SupportsIndex] | HasValue[int]) -> Computed[str]
__getitem__(key: HasValue[K]) -> Computed[V]
__getitem__(key: HasValue[K]) -> Computed[V]
__getitem__(key: Any) -> Computed[Any]
Return a reactive value for the item or slice of self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The index or slice to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__setattr__
¶
Set an attribute on the underlying self.value.
Note
It is necessary to set the attribute via the Signal, rather than the
underlying signal.value, to properly notify downstream observers
of changes. Reason being, mutable objects that, for example, fallback
to id comparison for equality checks will appear as if nothing changed
even if one of its attributes changed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the attribute to access. |
required |
value
|
Any
|
The value to set it to. |
required |
Example
>>> class Person:
... def __init__(self, name: str):
... self.name = name
... def greet(self) -> str:
... return f"Hi, I'm {self.name}!"
>>> s = Signal(Person("Alice"))
>>> result = s.greet()
>>> result.value
"Hi, I'm Alice!"
>>> s.name = "Bob" # Modify attribute on Person instance through the reactive value s
>>> result.value
"Hi, I'm Bob!"
__setitem__
¶
Set an item on the underlying self.value.
Note
It is necessary to set the item via the Signal, rather than the
underlying signal.value, to properly notify downstream observers
of changes. Reason being, mutable objects that, for example, fallback
to id comparison for equality checks will appear as if nothing changed
even an element of the object is changed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The key to change. |
required |
value
|
Any
|
The value to set it to. |
required |
Example
```py
s = Signal([1, 2, 3]) result = computed(sum)(s) result.value 6 s[1] = 4 result.value 8
where
¶
Return a reactive value for a if self is True, else b.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
HasValue[A]
|
The value to return if |
required |
b
|
HasValue[B]
|
The value to return if |
required |
Returns:
| Type | Description |
|---|---|
Computed[A | B]
|
A reactive value for |
subscribe
¶
Subscribe an observer to this variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observer
|
Observer
|
The observer to subscribe. |
required |
unsubscribe
¶
Unsubscribe an observer from this variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observer
|
Observer
|
The observer to unsubscribe. |
required |
observe
¶
Subscribe the observer (self) to all items that are Observable.
This method handles arbitrarily nested iterables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Any
|
A single item, an iterable, or a nested structure of items to potentially subscribe to. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
self |
unobserve
¶
Unsubscribe the observer (self) from all items that are Observable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Any
|
A single item or an iterable of items to potentially unsubscribe from. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
self |
__format__
¶
Format the variable with custom display options.
Format options: :n - just the name (or type+id if unnamed) :d - full debug info empty - just the value in brackets (default)
Computed
¶
Bases: Variable[T]
Read-only reactive value derived from a computation.
Computed recalculates its value whenever one of its observed
dependencies updates. In most usage, instances are created implicitly via
:func:computed, operator overloads, or helper APIs such as
:func:reactive_method.
Unlike :class:Signal, Computed.value is read-only and is updated by
re-running the stored function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[], T]
|
Zero-argument function used to compute the current value. |
required |
dependencies
|
Any
|
Dependencies to observe. May be a single item or nested container structure. |
None
|
Example
__getattr__
¶
__getattr__(name: str) -> Computed[Any]
Create a reactive value for retrieving an attribute from self.value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the attribute to access. |
required |
Returns:
| Type | Description |
|---|---|
Union[T, Computed[Any]]
|
A reactive value for the attribute access. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the attribute doesn't exist. |
Note
Type inference is poor whenever __getattr__ is used.
__call__
¶
Create a reactive value for calling self.value(*args, **kwargs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
P.args
|
Positional arguments to pass to the callable value. |
()
|
**kwargs
|
P.kwargs
|
Keyword arguments to pass to the callable value. |
{}
|
Returns:
| Type | Description |
|---|---|
Computed[R]
|
A reactive value for the function call. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the value is not callable. |
__abs__
¶
as_bool
¶
Return a reactive value for the boolean value of self.
Note
__bool__ cannot be implemented to return a non-bool, so it is provided as a method.
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__str__
¶
Return a string of the current value.
Note
This is not reactive.
Returns:
| Type | Description |
|---|---|
str
|
A string representation of |
__round__
¶
__round__() -> Computed[int]
__round__(ndigits: None) -> Computed[int]
__round__(ndigits: int) -> Computed[int]
__round__() -> Computed[int]
__round__(ndigits: None) -> Computed[int]
__round__(ndigits: int) -> Computed[int]
__round__() -> Computed[int]
__round__(ndigits: None) -> Computed[int]
__round__(ndigits: int) -> Computed[float]
__ceil__
¶
Return a reactive value for the ceiling of self.
Returns:
| Type | Description |
|---|---|
Computed[int]
|
A reactive value for |
__floor__
¶
Return a reactive value for the floor of self.
Returns:
| Type | Description |
|---|---|
Computed[int]
|
A reactive value for |
__invert__
¶
__neg__
¶
__pos__
¶
__trunc__
¶
__add__
¶
__add__(other: HasValue[int] | HasValue[float]) -> Computed[float]
__add__(other: HasValue[float]) -> Computed[float]
__add__(other: HasValue[Y]) -> Computed[R]
__add__(other: Any) -> Computed[Any]
Return a reactive value for the sum of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to add. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__and__
¶
Return a reactive value for the bitwise AND of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to AND with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
contains
¶
Return a reactive value for whether other is in self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to check for containment. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__divmod__
¶
__divmod__(other: HasValue[int]) -> Computed[tuple[int, int]]
__divmod__(other: HasValue[bool] | HasValue[int]) -> Computed[tuple[int, int]]
__divmod__(other: Any) -> Computed[tuple[float, float]]
is_not
¶
Return a reactive value for whether self is not other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value is not other. |
eq
¶
Return a reactive value for whether self equals other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value == other. |
Note
We can't overload __eq__ because it interferes with basic Python operations.
__floordiv__
¶
__floordiv__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__floordiv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the floor division of self by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for self.value // other.value. |
__ge__
¶
Return a reactive value for whether self is greater than or equal to other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value >= other. |
__gt__
¶
Return a reactive value for whether self is greater than other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for self.value > other. |
__le__
¶
Return a reactive value for whether self is less than or equal to other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__lt__
¶
Return a reactive value for whether self is less than other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__lshift__
¶
Return a reactive value for self left-shifted by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The number of positions to shift. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__matmul__
¶
Return a reactive value for the matrix multiplication of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to multiply with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__mod__
¶
__mod__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__mod__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self modulo other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__mul__
¶
__mul__(other: HasValue[int]) -> Computed[str]
__mul__(other: HasValue[int]) -> Computed[list[V]]
__mul__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the product of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to multiply with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__ne__
¶
Return a reactive value for whether self is not equal to other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to compare against. |
required |
Returns:
| Type | Description |
|---|---|
Computed[bool]
|
A reactive value for |
__or__
¶
Return a reactive value for the bitwise OR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to OR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rshift__
¶
Return a reactive value for self right-shifted by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The number of positions to shift. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__pow__
¶
__pow__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__pow__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self raised to the power of other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The exponent. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__sub__
¶
Return a reactive value for the difference of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to subtract. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__truediv__
¶
__truediv__(other: HasValue[int]) -> Computed[float]
__truediv__(other: HasValue[float]) -> Computed[float]
__truediv__(other: HasValue[int]) -> Computed[float]
__truediv__(other: HasValue[float]) -> Computed[float]
__truediv__(other: HasValue[bool] | HasValue[int] | HasValue[float]) -> Computed[float]
__truediv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self divided by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__xor__
¶
Return a reactive value for the bitwise XOR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to XOR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__radd__
¶
__radd__(other: HasValue[int] | HasValue[float]) -> Computed[float]
__radd__(other: HasValue[_SupportsAdd[T, R]]) -> Computed[R]
__radd__(other: Any) -> Computed[Any]
Return a reactive value for the sum of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to add. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rand__
¶
Return a reactive value for the bitwise AND of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to AND with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rdivmod__
¶
__rdivmod__(other: HasValue[int]) -> Computed[tuple[int, int]]
__rdivmod__(other: HasValue[int]) -> Computed[tuple[int, int]]
__rdivmod__(other: HasValue[bool]) -> Computed[tuple[int, int]]
__rdivmod__(other: Any) -> Computed[tuple[float, float]]
__rfloordiv__
¶
__rfloordiv__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__rfloordiv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the floor division of other by self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the numerator. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rmod__
¶
__rmod__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__rmod__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for other modulo self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The dividend. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rmul__
¶
__rmul__(other: HasValue[int]) -> Computed[str]
__rmul__(other: HasValue[int]) -> Computed[list[V]]
__rmul__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for the product of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to multiply with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__ror__
¶
Return a reactive value for the bitwise OR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to OR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rpow__
¶
__rpow__(other: HasValue[bool] | HasValue[int]) -> Computed[int]
__rpow__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self raised to the power of other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The base. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rsub__
¶
Return a reactive value for the difference of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to subtract from. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__rtruediv__
¶
__rtruediv__(other: HasValue[int]) -> Computed[float]
__rtruediv__(other: HasValue[float]) -> Computed[float]
__rtruediv__(other: HasValue[int]) -> Computed[float]
__rtruediv__(other: HasValue[float]) -> Computed[float]
__rtruediv__(other: HasValue[bool] | HasValue[int] | HasValue[float]) -> Computed[float]
__rtruediv__(other: HasValue[Y]) -> Computed[T | Y]
Return a reactive value for self divided by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The value to use as the divisor. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__rxor__
¶
Return a reactive value for the bitwise XOR of self and other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
HasValue[Y]
|
The value to XOR with. |
required |
Returns:
| Type | Description |
|---|---|
Computed[T | Y]
|
A reactive value for |
__getitem__
¶
__getitem__(key: slice) -> Computed[list[V]]
__getitem__(key: slice) -> Computed[tuple[V, ...]]
__getitem__(key: slice) -> Computed[str]
__getitem__(key: HasValue[SupportsIndex] | HasValue[int]) -> Computed[V]
__getitem__(key: HasValue[SupportsIndex] | HasValue[int]) -> Computed[V]
__getitem__(key: HasValue[SupportsIndex] | HasValue[int]) -> Computed[str]
__getitem__(key: HasValue[K]) -> Computed[V]
__getitem__(key: HasValue[K]) -> Computed[V]
__getitem__(key: Any) -> Computed[Any]
Return a reactive value for the item or slice of self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The index or slice to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Computed[Any]
|
A reactive value for |
__setattr__
¶
Set an attribute on the underlying self.value.
Note
It is necessary to set the attribute via the Signal, rather than the
underlying signal.value, to properly notify downstream observers
of changes. Reason being, mutable objects that, for example, fallback
to id comparison for equality checks will appear as if nothing changed
even if one of its attributes changed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the attribute to access. |
required |
value
|
Any
|
The value to set it to. |
required |
Example
>>> class Person:
... def __init__(self, name: str):
... self.name = name
... def greet(self) -> str:
... return f"Hi, I'm {self.name}!"
>>> s = Signal(Person("Alice"))
>>> result = s.greet()
>>> result.value
"Hi, I'm Alice!"
>>> s.name = "Bob" # Modify attribute on Person instance through the reactive value s
>>> result.value
"Hi, I'm Bob!"
__setitem__
¶
Set an item on the underlying self.value.
Note
It is necessary to set the item via the Signal, rather than the
underlying signal.value, to properly notify downstream observers
of changes. Reason being, mutable objects that, for example, fallback
to id comparison for equality checks will appear as if nothing changed
even an element of the object is changed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The key to change. |
required |
value
|
Any
|
The value to set it to. |
required |
Example
```py
s = Signal([1, 2, 3]) result = computed(sum)(s) result.value 6 s[1] = 4 result.value 8
where
¶
Return a reactive value for a if self is True, else b.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
HasValue[A]
|
The value to return if |
required |
b
|
HasValue[B]
|
The value to return if |
required |
Returns:
| Type | Description |
|---|---|
Computed[A | B]
|
A reactive value for |
subscribe
¶
Subscribe an observer to this variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observer
|
Observer
|
The observer to subscribe. |
required |
unsubscribe
¶
Unsubscribe an observer from this variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observer
|
Observer
|
The observer to unsubscribe. |
required |
observe
¶
Subscribe the observer (self) to all items that are Observable.
This method handles arbitrarily nested iterables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Any
|
A single item, an iterable, or a nested structure of items to potentially subscribe to. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
self |
unobserve
¶
Unsubscribe the observer (self) from all items that are Observable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Any
|
A single item or an iterable of items to potentially unsubscribe from. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
self |
__format__
¶
Format the variable with custom display options.
Format options: :n - just the name (or type+id if unnamed) :d - full debug info empty - just the value in brackets (default)
computed
¶
Wrap a function so calls produce a reactive Computed result.
The returned wrapper accepts plain values, reactive values, or nested
containers that include reactive values. On each recomputation, arguments
are normalized with :func:deep_unref, so func receives plain Python
values.
The created :class:Computed subscribes to reactive dependencies found in
args and kwargs at call time. When any dependency updates, the
function is re-evaluated and subscribers are notified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., R]
|
Function that computes a derived value from its inputs. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., Computed[R]]
|
A wrapper that returns a :class: |
unref
¶
Resolve a value by unwrapping reactive containers until plain data remains.
This utility repeatedly unwraps :class:Variable objects by following
their internal _value references, allowing callers to operate on the
underlying Python value regardless of nesting depth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
HasValue[T]
|
Plain value, reactive value, or nested reactive value. |
required |
Returns:
| Type | Description |
|---|---|
T
|
The fully unwrapped value. |
has_value
¶
Check whether an object's resolved value is an instance of type_.
This helper is a typed guard around :func:unref. It is useful when code
accepts either plain values or reactive values and needs a narrowed type
before continuing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Value to inspect. May be plain or reactive. |
required |
type_
|
type[T]
|
Expected resolved value type. |
required |
Returns:
| Type | Description |
|---|---|
TypeGuard[HasValue[T]]
|
|
TypeGuard[HasValue[T]]
|
|
deep_unref
¶
Recursively resolve reactive values within nested containers.
deep_unref is the structural counterpart to :func:unref. It unwraps
reactive values that appear inside supported containers while preserving the
container type where practical.
Supported behavior:
- scalar primitives are returned unchanged
- reactive values are unwrapped recursively
- dict, list, and tuple contents are recursively unwrapped
- generic iterables are reconstructed when possible; otherwise returned as-is
- numpy.ndarray values with dtype=object are recursively unwrapped
element-wise
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Any value, possibly containing reactive values. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Value with reactive nodes recursively replaced by plain values. |
reactive_method
¶
Decorate an instance method so calls return a Computed value.
The decorated method keeps its original call signature but now returns a
reactive value. Dependencies include:
- instance attributes named in dep_names (when present), and
- call-time args and kwargs.
This is useful for class APIs where a derived value depends on reactive
fields owned by self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*dep_names
|
str
|
Attribute names on |
()
|
Returns:
| Type | Description |
|---|---|
Callable[[InstanceMethod[P, T]], ReactiveMethod[P, T]]
|
A decorator that transforms an instance method into one that returns |
Callable[[InstanceMethod[P, T]], ReactiveMethod[P, T]]
|
class: |
Example
>>> from signified import Signal, reactive_method
>>> class Counter:
... def __init__(self):
... self.count = Signal(1)
... @reactive_method("count")
... def doubled(self):
... return self.count.value * 2
>>> c = Counter()
>>> result = c.doubled()
>>> result.value
2
>>> c.count.value = 4
>>> result.value
8
as_signal
¶
Normalize a value to a signal-compatible reactive object.
If val is already reactive, it is returned unchanged to avoid wrapping
an existing reactive node. Otherwise a new :class:Signal is created.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
HasValue[T]
|
Plain value or reactive value. |
required |
Returns:
| Type | Description |
|---|---|
Signal[T]
|
A reactive value suitable for APIs expecting |
Note
Existing reactive values are returned as-is at runtime, including
Computed instances.