Complete worked example
python
class Score:
def __init__(self, value):
self.value = value
@property
def value(self):
return self._value
@value.setter
def value(self, value):
if type(value) is not int or value < 0:
raise ValueError('score must be a nonnegative integer')
self._value = value
def inspect_example(initial, updated):
score = Score(initial)
try:
score.value = updated
error = None
except ValueError:
error = 'ValueError'
return {'value': score.value, 'error': error}- Reasoning
- Construction assigns self.value, which invokes the setter instead of directly storing the input. The getter later reads _value. During an update, the function attempts the same public attribute assignment, catches its domain ValueError and reports the remaining value.