Skip to content

Derived

didactic.api.derived

derived(fn: Callable[..., FieldValue]) -> property

Mark a method as a cached derivation on a Model.

Parameters:

Name Type Description Default
fn Callable[..., FieldValue]

The method to mark. Takes self and returns a value.

required

Returns:

Type Description
property

A descriptor that returns the cached value on access. The cache lives on the instance under __derived_values__ and is populated by the metaclass after construction.

Examples:

>>> import didactic.api as dx
>>> class Box(dx.Model):
...     w: int
...     h: int
...
...     @dx.derived
...     def area(self) -> int:
...         return self.w * self.h
>>> Box(w=3, h=4).area
12