Function Contracts
& Invariants
Learn to state clear input/output contracts and invariants for Python functions: the promises your code makes and keeps.
What is a Function Contract?
A function contract is a precise agreement between the function and its caller. It specifies two things: what the function requires from the caller (inputs), and what it guarantees in return (outputs).
The Contract Pipeline
Why Contracts Matter
Without contracts, functions become black boxes with hidden assumptions. Contracts make those assumptions explicit, preventing bugs before they happen.
Formal Definitions
Anatomy of a Contract in Python
Preconditions vs Postconditions
| Precondition | Postcondition | |
|---|---|---|
| When checked | Before execution | After execution |
| Who is responsible | Caller | Function |
| If violated | Caller's bug | Function's bug |
| Python tool | assert at start |
assert before return |
| Example | \( n > 0 \) | \( \text{result} \geq 0 \) |
Understanding Invariants
An invariant is a condition that stays true throughout a function's execution. While preconditions and postconditions are checked at the boundaries, invariants describe properties that never change no matter what happens inside.
How to Write a Contract
assert statements or docstring clauses.assert before the return.Example 1: Square Root
Example 2: Binary Search
Example 3: Withdraw from Account
Loop Invariants Explained
Common Mistakes
Summary Table
| Precondition | Postcondition | Invariant | |
|---|---|---|---|
| What it says | "Input must be X" | "Output will be Y" | "Property Z always holds" |
| Where in code | Top of function | Before return | Throughout body |
| Python tool | assert, type hint |
assert, type hint |
Docstring, design |
| Blame on violation | Caller | Function | Function |
| Example | \(\text{len(arr)} > 0\) | \(\text{result} \geq 0\) | arr unchanged |
Question 1 of 5
A function requires that its input list is non-empty. This requirement is a:
Question 2 of 5
Who is at fault if a postcondition is violated?
Question 3 of 5
def find_max(nums: list[int]) -> int:
"""
Pre: nums is non-empty
Post: \( \text{result} \geq \) every element in nums
Post: result is an element of nums
Inv: nums is not modified
"""
Which of the following calls violates the contract?
[] violates this precondition. The other calls all provide at least one element, satisfying the contract.Question 4 of 5
In a binary search function, the statement "if the target exists, it lies within \(\text{arr}[\text{lo}..\text{hi}]\)" is best described as a:
Question 5 of 5
def deposit(balance: float, amount: float) -> float:
"""
Pre: \( \text{balance} \geq 0 \)
Pre: \( \text{amount} > 0 \)
Post: \( \text{result} = \text{balance} + \text{amount} \)
Post: \( \text{result} > \text{balance} \)
"""
return balance + amount
Which invariant would best complement this contract?
balance and amount are floats (immutable in Python), the no-mutation invariant is trivially satisfied but still worth documenting for clarity.