Follow the transformation
Put parsing/file/user interaction at the boundary.
Pass clean values into calculation functions.
Return results instead of printing from the core logic.
Pure logic receives values and returns values without reading files, printing, prompting users or depending on hidden mutable state.
Pure logic receives values and returns values without reading files, printing, prompting users or depending on hidden mutable state. Separating this logic from input/output code makes calculations easier to test and reuse because tests can supply ordinary values instead of reproducing files, terminals or network calls.
Learning goal: explain why Separate Pure Logic from I O behaves this way, apply it to a small example, and verify the result independently. Begin by being able to justify this first step: Put parsing/file/user interaction at the boundary.
Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Put parsing/file/user interaction at the boundary. Stage 2: Pass clean values into calculation functions. Stage 3: Return results instead of printing from the core logic. Final checkpoint: Test the pure function with normal and edge-case inputs.
Put parsing/file/user interaction at the boundary.
Pass clean values into calculation functions.
Return results instead of printing from the core logic.
# Step 1 — Define the reusable `mean` function; its indented body describes what happens for each call.
def mean(values):
# Step 2 — Return the computed value to the caller so the result can be reused or tested.
return sum(values) / len(values)
# Step 3 — Compute the right-hand expression and store its result in `result` for the next step.
result = mean([2, 4, 6])
# Step 4 — Display the current value explicitly so the result/state can be inspected during execution.
print(f"mean={result:.1f}")mean() contains only the calculation; print() is an I/O concern in the calling layer.
For Separate Pure Logic from I O, trace the specific input through the mechanism above and independently verify one returned value, state change or side effect.
InputObjects/values supplied to the operation.StateNames or mutable objects that may change during execution.OutputReturned value, side effect, file, plot or exception to inspect.Use Separate Pure Logic from I O when it answers a defined question in Object-Oriented Python and its inputs/assumptions match the current data or program state.
Reconsider Separate Pure Logic from I O when the required information is unavailable, the operation would violate a validation/data boundary, or a simpler operation answers the question more transparently.
Construct a tiny example of Separate Pure Logic from I O. First put parsing/file/user interaction at the boundary. Then pass clean values into calculation functions. Predict the result before execution and explain one boundary or failure case.
Which approach best demonstrates understanding of Separate Pure Logic from I O?
Step 1Put parsing/file/user interaction at the boundary.Step 2Pass clean values into calculation functions.Step 3Return results instead of printing from the core logic.Step 4Keep formatting/export in a separate boundary function.