Pylerno
English

Pure functions

Keep business calculations independent of input, output, and storage.

On this page

The concept

A pure function returns the same result for the same input and does not change external state. This is useful for tests because the result does not depend on when, where, or how often the function runs. Not every function must be pure: file writing and user interaction are necessary at the boundaries.

A practical design keeps a small pure core and a visible I/O layer. The core transforms data. The outer layer reads data, calls the core, and displays or stores the answer. This separation works with ordinary functions and modules; it does not require OOP.

Worked example

python
def add_bonus(scores, bonus):
    return [min(100, score + bonus) for score in scores]


scores = [70, 95]
updated = add_bonus(scores, 10)
print("Original:", scores)
print("Updated:", updated)
print("Repeated:", add_bonus(scores, 10))

Trace the result

  1. add_bonus creates a new list and caps each result at 100.
  2. Printing the original list confirms that the input was not mutated.
  3. Calling the function again with identical input produces the same updated values.
text
Original: [70, 95]
Updated: [80, 100]
Repeated: [80, 100]
Line-by-line walkthrough

Lines 1–2: def add_bonus(scores, bonus):

Define the function add_bonus with input names scores, bonus. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.

Line 2: return [min(100, score + bonus) for score in scores]

Evaluate [min(100, score + bonus) for score in scores] and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.

Line 5: scores = [70, 95]

Evaluate [70, 95], then bind or update scores with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.

Line 6: updated = add_bonus(scores, 10)

Evaluate add_bonus(scores, 10), then bind or update updated with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.

Line 7: print("Original:", scores)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 8: print("Updated:", updated)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 9: print("Repeated:", add_bonus(scores, 10))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

A function named calculate should not unexpectedly write a file. Make side effects visible through names, structure, and Markdown documentation.

Check your understanding

What does this program print?

python
a = [3, 1]
b = sorted(a)
print(a[0])
Choose one answer

Search documentation

Share this page