Separate Planning from Applying in Dry Runs
Core idea
A dry run describes proposed effects without performing them. Separate a planning function from the applying function and ensure preview paths cannot reach the mutation step. A preview is not a guarantee: real state may change before execution.
stock = {"A": 2, "B": 0}plan = [key for key, n in stock.items() if n == 0]print(plan)print(stock)Planning creates a list without altering the stock.
Gate an in-memory effect
def run(stock, preview): plan = [k for k, n in stock.items() if n == 0] if not preview: for k in plan: del stock[k] return planstock = {"A": 0}print(run(stock, True), stock)print(run(stock, False), stock)The same plan is reported, but only apply mode mutates.
Common mistake
Do not perform writes, network changes or destructive cleanup while merely computing a preview. Recheck preconditions before applying a stale plan.
Practice
Add a preview test that checks both the returned plan and unchanged input dictionary.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dry-run boundaries
Check your understanding of dry-run workflows.
What is printed?
d={"a":0}plan=[k for k,v in d.items() if v==0]print(len(d),len(plan))What is printed?
items=[]preview=Trueif not preview: items.append("write")print(items)What should dry-run mode avoid?
Why recheck before applying a plan?