Complete worked example
python
def valid_value(value):
return type(value) is int and 1 <= value <= 3- Reasoning
- Exact int deliberately rejects bool under this domain contract.
Typorithm · easy
Step 1 of 4 · intro card
Use object and type semantics deliberately instead of treating every numeric-looking value as interchangeable.
Names refer to objects; assignment binds a name rather than copying an arbitrary object graph. A value also has a runtime type. The worked function accepts only exact integers in the inclusive range one to three and returns a boolean classification without converting strings or changing its input.
The word exact is intentional. Python bool is a subclass of int, so True can satisfy an isinstance integer test even though the page-limit contract must reject booleans. A type boundary should follow the domain rule rather than a convenient resemblance between values.
The function first requires type(value) is int, then checks the inclusive numeric range.
The function first requires type(value) is int, then checks the inclusive numeric range. Short-circuit and prevents the range comparison from being evaluated for an unsuitable type. Inputs 1, 2 and 3 are valid; 0, 4, 1.5, a numeric string and True are not.
This is one specific validation policy, not a universal instruction to avoid isinstance. When subclass instances are intended, isinstance is usually the appropriate tool. State whether the contract accepts a numeric family or a strict primitive representation before choosing the check.
python
def valid_value(value):
return type(value) is int and 1 <= value <= 3The counterexample uses isinstance(value, int) without excluding bool, accepting True as a page limit.
The counterexample uses isinstance(value, int) without excluding bool, accepting True as a page limit. A separate common mistake converts every input with int first, turning a fraction into a truncated integer or admitting representations the caller never intended to accept.
Use negative cases that look deceptively plausible: booleans, decimal values, strings and boundary neighbors. Test exact boundaries as well as interior values. A fixture containing only 2 and 0 would not reveal either the boolean-subclass issue or an accidentally exclusive endpoint.
python
def valid_value(value):
return isinstance(value, int) and 1 <= value <= 3Adapt the validator to exact integer experience values from zero through fifty.
Adapt the validator to exact integer experience values from zero through fifty. Zero is valid for this contract, even though it was invalid for the worked page-limit contract. Keep booleans, fractions and strings invalid and return a boolean rather than raising for these classification cases.
Change the two numeric boundaries without weakening the exact-type rule. Predict results for zero, fifty, minus one and fifty-one before running the test. This task develops a small reusable condition rather than introducing a parser and a new error policy simultaneously.
Step 1: Keep the exact-type predicate. Step 2: Use inclusive zero and fifty boundaries.
python
def valid_value(value):
# Exact integers, including zero, through fifty.
raise NotImplementedErrorThe solution accepts zero and fifty and rejects their outside neighbors.
The solution accepts zero and fifty and rejects their outside neighbors. It also rejects True and the string twenty. Zero is not treated as missing or false eligibility: its validity follows from the explicit range, not from a generic truthiness check.
Tests check type distinctions independently of the range. A wrong implementation can admit a boolean while getting every ordinary integer case right. Keeping that adversarial case makes the exercise useful when the code is later refactored into a larger input boundary.
python
def valid_value(value):
return type(value) is int and 0 <= value <= 50In an interview, explain bool's relationship to int without claiming that all subclasses are invalid everywhere.
In an interview, explain bool's relationship to int without claiming that all subclasses are invalid everywhere. Also distinguish assignment, conversion and validation: they solve different problems. Type hints alone do not execute this runtime predicate, a distinction revisited in the class lesson.
The next lesson uses string normalization and Unicode case folding. It applies the same discipline: define the accepted input domain and comparison contract before selecting a convenient operation that might silently change its meaning.
Optional stretch — after the core task works: Compare an intentional subclass-accepting validator with this strict primitive contract.
Computer practice: https://applydjinn.com/typorithm/python-lab-20261105.zip. Run verify.py with your selected Python interpreter, then complete an exercise and check it with --practice followed by the lesson id. The lab uses only the standard library and disposable local fixtures; it never connects to the production database or company feeds.
Use object and type semantics deliberately instead of treating every numeric-looking value as interchangeable.