Bug Fix

The Sticky Default

The Sticky Default

Bug Report

A function is supposed to add an item to a fresh shopping cart every time it's called without an explicit list. Instead, items from previous calls keep showing up in the 'new' cart.

Mutable DefaultsFunction ArgumentsCommon Gotcha
Fix the Bug: The Sticky Default

In Python, default argument values are evaluated only ONCE, when the function is defined — not every time it's called. If you use a mutable object (like a list or dict) as a default value, that SAME object is reused and mutated across every call that relies on the default.

EditorPython · JSX
PreviewUpdates on Run Tests
Loading preview…
Tests
Should use 'cart=None' as the default parameter
Should check 'if cart is None' and create a new list
Should NOT use a mutable list literal as the default