Complete worked example
python
from functools import singledispatch
@singledispatch
def normalize(value):
raise TypeError('unsupported value')
@normalize.register
def _(value: int):
return value * 2
@normalize.register
def _(value: bool):
raise TypeError('boolean is not a score')
@normalize.register
def _(value: str):
return value.strip()
def inspect_example(values):
result = []
for value in values:
try:
result.append(normalize(value))
except TypeError:
result.append('unsupported')
return result- Reasoning
- The default normalize raises TypeError. Registered integer values are doubled, strings are stripped and booleans reach their dedicated rejection branch. The outer reporting function converts only those deliberate TypeErrors into an unsupported marker while preserving input order.