Classes and objects
Define the ThresholdModel class input contract
Page 2 hardens the boundary around the ThresholdModel class with per-instance cutoffs so bad inputs fail before the interesting algorithm runs.
1Learn the idea
Read
Define what may enter
The accepted input remains: model name, threshold, and numeric scores passed to predict. Keep parsing and normalization in functions that do not score, train, or call a model. That split lets a test fail the boundary without blaming the core logic. The user-facing decision stays: reuse one predict method across two named models with different thresholds.
Read
Reject at the boundary
class ThresholdModel:
def __init__(self, name, threshold):
if not 0<=threshold<=1: raise ValueError('threshold out of range')
self.name=name; self.threshold=threshold
print(ThresholdModel('Strict',0.8).threshold)
Expected evidence: 0.8. If the contract is silent on a bad value, later debugging will look like an algorithm bug when it is really a data bug.
Read
Keep transforms testable
Write one assertion for a neighboring valid input to the ThresholdModel class so tightening the boundary does not over-reject. Document field names and types the way a teammate would need them on day two of python-classes—not as comments you plan to delete.
Read
Lab notebook: name the fields
List every field in Strict(threshold=0.8), Loose(threshold=0.4), scores=[0.5,0.9] and mark each as required, optional, or forbidden. Required fields must fail loudly when missing; optional fields need defaults you can quote in a test; forbidden fields (secrets, raw PII, path escapes) must never be accepted silently. This list is the contract for the ThresholdModel class.
Add one sentence about encoding, units, or timezones if relevant to model name, threshold, and numeric scores passed to predict. Contracts that ignore units create “correct” programs that still ship wrong decisions when someone tries to reuse one predict method across two named models with different thresholds.
Read
Worked judgment
Write the error string you want for the most likely bad input. Prefer ValueError('threshold out of range')-style messages over generic invalid input. The contract’s job is to make shared mutable class attribute for threshold so instances overwrite each other harder to confuse with a model or algorithm bug later.
Read
Why this stage matters for the ThresholdModel class
At the data contract stage for python-classes, the job is narrower than finishing a product demo. You are creating one progressive evidence piece about Strict(threshold=0.8), Loose(threshold=0.4), scores=(0.5,0.9) that later pages inherit without redefining success. Keep that fixture small enough to inspect by hand, keep outputs copy-pasteable as text, and refuse to narrate this baseline as if it were a production SLA: two handwritten prediction tables before coding the class.
For this page specifically, success looks like malformed inputs rejected with field-named errors while still centering the user decision to reuse one predict method across two named models with different thresholds. If you cannot point to a file, command, or assertion that proves that for the ThresholdModel class, stay on this page instead of advancing.
Go deeper
Before you start
Why this matters
Invent one malformed input that the ThresholdModel class with per-instance cutoffs might accidentally accept. Predict the exception or rejection message. After you run the contract code, compare your prediction with the real failure text.
In the wild
See how this idea shows up as a product and a company — then come back to the lesson. Skills transfer across vendors.
Related lessons
Check your understanding
Page assessment
Answer from memory. Completion is saved from this evidence, not from opening the next page.
All responses are required.