Value Objects¶
A ValueObject is an immutable object with value-based equality and hashing.
Characteristics¶
- Immutable after construction
- Equality determined by all field values, not identity
- Hashable — suitable for use as dict keys or set members
- Optionally serializable via
__dict__without extra ceremony
When to use¶
Use ValueObject when you need immutability, value-based __eq__/__hash__, and optional __dict__ serialization in one base class. Useful for any type where identity is irrelevant and field values define equality.
ValueObject vs auto-freeze
ValueObject gives you immutability, value equality, and hashing.
If you only need immutability and already have your own base class, auto-freeze is a lighter alternative. Both are optional.
Related
The Domain block re-exports ValueObject. See Domain Value Objects for domain-specific guidance.