Value Objects in Python — ELI5

Think about a five-dollar bill. You do not care which specific five-dollar bill is in your wallet — any five-dollar bill is the same as any other. If someone swaps yours for a different one, you do not notice or mind. What matters is the value: five dollars.

Now think about your pet dog. Your dog is unique. Even if another dog looks identical, it is not your dog. Your dog has an identity — a name, a history, a personality.

In programming, value objects are like the dollar bill. They are defined entirely by their contents. Two objects with the same values are equal, interchangeable, and identical in every way that matters.

Here are some everyday examples of value objects:

  • An address: 123 Main Street, Springfield — if two objects hold these same details, they represent the same address
  • A color: red is red, regardless of which variable holds it
  • A date range: March 1 to March 15 is the same period no matter how many times you write it down
  • A money amount: $19.99 USD is $19.99 USD

Value objects also never change after you create them. You do not modify a date — you create a new date. You do not change the amount on a dollar bill — you get a different bill.

This might sound like a small idea, but it prevents a huge number of bugs. When things cannot be changed accidentally, your code becomes much safer and easier to understand.

The one thing to remember: Value objects are defined by their values, not their identity — two with the same data are equal, and they never change once created.

pythonarchitectureddd

See Also