Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. class OneDigitNumericValue:
  2. def __init__(self, default_val=5):
  3. self.value = default_val
  4.  
  5. def __add__(self, other_value):
  6. if not isinstance(other_value, int):
  7. raise AttributeError("The value is not an integer.")
  8. new_value = self.value + other_value
  9.  
  10. if not (0 < new_value < 9):
  11. raise AttributeError("The value is not between 0 and 9.")
  12.  
  13. return new_value
  14.  
  15. def __repr__(self):
  16. return str(self.value)
  17.  
  18.  
  19. class Person:
  20. moods = {
  21. "happy": OneDigitNumericValue(),
  22. "angry": OneDigitNumericValue(),
  23. "sad": OneDigitNumericValue()
  24. }
  25.  
  26. def update_states(self, state_updates: {}):
  27. for mood, val_change in state_updates.items():
  28. print('before assignment:', self.moods[mood])
  29. self.moods[mood] + val_change
  30. print('before assignment:', self.moods[mood])
  31. self.moods[mood] += val_change
  32. print("after assignment:", self.moods[mood])
  33.  
  34.  
  35. p = Person()
  36.  
  37. for mood, val in p.moods.items():
  38. print(mood, val)
  39.  
  40. p.update_states({
  41. "happy": 2,
  42. "sad": -2
  43. })
  44.  
  45. for mood, val in p.moods.items():
  46. print(mood, val)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement