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']
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']
this step needs the editor
on desktop today; in the app (coming soon). save your spot and we'll bring you back here when you're ready.