Skip to content

Instantly share code, notes, and snippets.

@wuub
Forked from tauzen/money.py
Created March 28, 2017 08:05
Show Gist options
  • Save wuub/b455b2c9945b7003958fa25ebe0d71e9 to your computer and use it in GitHub Desktop.
Save wuub/b455b2c9945b7003958fa25ebe0d71e9 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
@vleandersson
Copy link

🙏

@sevysevy
Copy link

m1 == m should be False i mind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment