cd /news/developer-tools/mutable-default-arguments-in-python-… · home topics developer-tools article
[ARTICLE · art-129113] src=lernerpython.com ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Mutable default arguments in Python: why `__defaults__` bites

A Python programming note published September 2, 2026 warns that mutable default arguments persist across calls because function defaults are stored in the __defaults__ attribute, so a function defined as def add1(x=[]) returns [1], then [1, 1], then [1, 1, 1] on successive calls. The example shows add1.__defaults__ holding ([1, 1, 1],), demonstrating that x.append(1) mutates the shared default object rather than creating a fresh list.

by read1 min views13 publishedSep 2, 2026
Mutable default arguments in Python: why `__defaults__` bites
Image: Lernerpython (auto-discovered)

Mutable default arguments in Python: why __defaults__ bites

September 2, 2026

Python

Python

Because Python functions’ defaults are kept in defaults, avoid mutable defaults:

def add1(x=[]):
    x.append(1) # same as: add1.__defaults__[0].append(1) ‼️
    return x
add1() # [1]
add1() # [1, 1]
add1() # [1, 1, 1] 🤯

add1.defaults # ([1, 1, 1],)

← Python generator frames: Inspecting locals with gi_frame

Claude Code always produces something. That’s the hard part. →

── more in #developer-tools 4 stories · sorted by recency
── more on @python 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/mutable-default-argu…] indexed:0 read:1min 2026-09-02 ·