Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class OneDigitNumericValue:
- def __init__(self, default_val=5):
- self.value = default_val
- def __add__(self, other_value):
- if not isinstance(other_value, int):
- raise AttributeError("The value is not an integer.")
- new_value = self.value + other_value
- if not (0 < new_value < 9):
- raise AttributeError("The value is not between 0 and 9.")
- return new_value
- def __repr__(self):
- return str(self.value)
- class Person:
- moods = {
- "happy": OneDigitNumericValue(),
- "angry": OneDigitNumericValue(),
- "sad": OneDigitNumericValue()
- }
- def update_states(self, state_updates: {}):
- for mood, val_change in state_updates.items():
- print('before assignment:', self.moods[mood])
- self.moods[mood] + val_change
- print('before assignment:', self.moods[mood])
- self.moods[mood] += val_change
- print("after assignment:", self.moods[mood])
- p = Person()
- for mood, val in p.moods.items():
- print(mood, val)
- p.update_states({
- "happy": 2,
- "sad": -2
- })
- for mood, val in p.moods.items():
- print(mood, val)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement