Provide a getter
and setter
for dataclasses.fields
to allow e.g. for lazy
evaluation or field content validation.
pip install znfields
The znfields.field
supports all arguments from dataclasses.field
with the
additional getter
argument.
import dataclasses
import znfields
def getter(self, name) -> str:
return f"{name}:{self.__dict__[name]}"
def setter(self, name, value) -> None:
if not isinstance(value, float):
raise ValueError(f"Value {value} is not a float")
self.__dict__[name] = value
@dataclasses.dataclass
class MyModel(znfields.Base):
parameter: float = znfields.field(getter=getter, setter=setter)