promptdojo_

Mutation and the action-at-a-distance bug — step 6 of 8

This is the single most famous Python bug — and AI ships it constantly.

The function add_pet uses pets=[] as a default. The trap: that empty list is created once, when the function is defined, not each time it's called. Every call without an argument shares the same list.

Run it as-is and you'll see the second call returns ["luna", "moose"] instead of just ["moose"]. Fix line 1 so each call without an argument gets a fresh list.

Expected output:

['luna']
['moose']
The break is on line 1 — but read the whole snippet first.

full-screen editor opens — close anytime to keep reading.