Why does Python use mutable default arguments cause bugs?
Variables, references, and memory model
Audio flashcard · 0:18Nortren·
Why does Python use mutable default arguments cause bugs?
0:18
Default arguments are evaluated only once, when the function is defined, not on each call. If the default is a mutable object like an empty list, all calls share the same list. Modifications persist across calls, causing surprising behavior. The fix is to use None as the default and create a fresh list inside the function.
docs.python.org