Backlucerna← Collection

Why does a supposedly empty basket still hold last time’s items?

pythonMutable default arguments
Source code
def add_item(item, basket=):
    basket.append(item)
    return basket
❯ Try editing the highlighted part of the code above
Swap basket=… between [] and None in the code above. With [] it accumulates across calls; with None every call starts fresh ── the result flips.
Memory / executionStep 1 / 5
👁 What you can see ── the supposedly empty basket keeps last time’s items
On the left is the whole function ── a perfectly ordinary one that just puts item into a basket and returns it (basket=[] is meant as an empty basket when omitted). Calling add_item("apple") gives ['apple']. Nothing surprising so far. ── But call add_item("banana") somewhere else, and the supposedly empty basket gives ['apple', 'banana']. The banana call never mentioned apple, yet last time’s item is still there. Shouldn’t basket start as an empty [] every call? Use ▶ to peek at the mechanism.
Use ← → keys too

Other pieces

One step beneath “it works.”

Backlucerna · Phase 1 MVP · Astro + React island