Computed¶
didactic.api.computed ¶
computed(
fn: Callable[..., FieldValue] | None = None,
*,
materialise: bool = False,
) -> (
property
| Callable[[Callable[..., FieldValue]], property]
)
Mark a method as a computed field on a Model.
| PARAMETER | DESCRIPTION |
|---|---|
fn
|
The method to mark. Supplied positionally when the decorator is
used without arguments (
TYPE:
|
materialise
|
Recorded on the underlying property's marker for downstream tooling (theory codegen, axiom emission). At the Python level this flag has no effect: the value is always recomputed on access, never stored.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
property or Callable
|
Either a |
Notes
The wrapped method should take self and return a value of any
didactic-supported type. Computed fields participate in
model_dump but never in storage.
Examples:
>>> import didactic.api as dx
>>> class Person(dx.Model):
... first_name: str
... last_name: str
...
... @dx.computed
... def full_name(self) -> str:
... return f"{self.first_name} {self.last_name}"
>>> p = Person(first_name="Ada", last_name="Lovelace")
>>> p.full_name
'Ada Lovelace'
>>> p.model_dump()["full_name"]
'Ada Lovelace'