Conceptual
Login

Stop Passing Primitives Everywhere: Use Value Objects in Python

The value object pattern, from domain-driven design, represents domain concepts as objects that are immutable, compared by value rather than identity, and responsible for enforcing their own invariants at construction, so that an instance, once created, can be trusted everywhere and invalid states become unrepresentable. It counters primitive obsession, where domain concepts (prices, percentages, email addresses) are modeled as bare primitives that carry no meaning, enforce no rules, and force every function to repeat defensive validation, leading to scattered rules and bugs. The pattern belongs to software design and domain modeling, and in Python can be implemented with frozen data classes or immutable subclasses of built-in types.