Python DDD - Value Object idea. Immutable attributes, methods, structural equality.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
🙏