I/O at boundaries

Lesson 9 of 20 · Functional Python: values, behavior, and choices

Input/output (I/O) connects a program to a terminal, file, network, or database. Reading may depend on changing external state; writing changes the environment. These operations are necessary, but we can put them at explicit boundaries and keep calculations separately callable.

def format_reading(text):
    cleaned = text.strip()
    return "Reading: " + cleaned

def show_reading(text):
    print(format_reading(text))

show_reading("  18 C  ")
Reading: 18 C

format_reading returns text; show_reading displays it. This separation lets a caller use the result without printing. In a file workflow the relationship could be:

read file  ->  transform contents  ->  write result
   I/O          calculation             I/O

Real boundaries must also handle errors such as a missing file or failed write. A function with no explicit return yields None, but may still print, mutate data, or raise an exception. Conversely, a calculation whose result is discarded and has no observable effect accomplishes no observable work on that successful call.

Chapter index