Complete worked example
python
from collections.abc import Sequence
class Scores(Sequence):
def __init__(self, values):
self._values = tuple(values)
def __len__(self):
return len(self._values)
def __getitem__(self, index):
return self._values[index]
def inspect_example(values, index):
scores = Scores(values)
try:
selected = scores[index]
except IndexError:
selected = 'out-of-range'
return {'item': selected, 'length': len(scores), 'containsZero': 0 in scores}- Reasoning
- The constructor takes an immutable snapshot of the declared numeric input sequence. Length delegates to that tuple, and indexing delegates the received index unchanged. The report catches an out-of-range access and records a label while independently measuring length and membership of zero.