Skip to content

Instantly share code, notes, and snippets.

@tauzen
Last active March 28, 2017 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tauzen/ea97bbe8ea4e558b4eda84ba0cac527f to your computer and use it in GitHub Desktop.
Save tauzen/ea97bbe8ea4e558b4eda84ba0cac527f to your computer and use it in GitHub Desktop.
Python DDD - Value Object idea. Immutable attributes, methods, structural equality.
from collections import namedtuple
class Money(namedtuple('Money', ['amount', 'currency'])):
def add(self, amount):
return Money(self.amount + amount, self.currency)
m = Money(20, 'USD')
print(m)
# Money(amount=20, currency='USD')
m.add(10)
# Money(amount=30, currency='USD')
m1 = Money(20, 'USD')
m2 = Money(20, 'PLN')
m1 == m
# True
m1 == m2
# False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment