Programming · Flashcard
f(v, acc=[]) appends v to acc. What does f(2) return after f(1)?
Why this is the answer
Default values are evaluated once, when the def statement runs, and stored on the function object — so both calls append to the same list and f returns [1, 2]. A fresh list per call is the common wrong mental model; the fix is acc=None plus acc = [] in the body. Threads play no part, and Python accepts mutable defaults without complaint.
Read more in the docs