The built-in [[Python]] library `dataclasses` offers a lightweight structure for capturing a data [[schema]].
For a more performant option (validation, enforcing type hints, etc.) see [[pydantic]].
```python
from dataclasses import dataclass
@dataclass
class Animal():
name: str
weight: float
dog = Animal(name="Fido", weight="50.0")
print(dog)
# Animal(name='Fido', weight=50.0)
assert dog.weight > 0
# True
```
Use `field(default_factory=list)` for mutable defaults — this ensures each instance gets its own list instead of sharing one.
```python
from dataclasses import dataclass, field
@dataclass
class Zoo:
name: str
animals: list[str] = field(default_factory=list)
z = Zoo("City Zoo")
z.animals.append("Lion")
print(z)
# Zoo(name='City Zoo', animals=['Lion'])
```