Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
IT Nachrichten22. September(22.09.2026 um 00:05 Uhr)
IT NachrichtenLizenzprobleme: AnyDesk und TeamViewer(22.09.2026 um 00:30 Uhr)
Apple iOS & macOSApple's iOS 27.2 beta 2 reveals new anti-snatching protections(22.09.2026 um 00:27 Uhr)
AI & KI NachrichtenUC Irvine to Study AI for Writing Instruction(21.09.2026 um 23:31 Uhr)
AI & KI NachrichtenBurnham to call for global effort to control threats posed by AI(21.09.2026 um 23:30 Uhr)
IT Nachrichten22. September(22.09.2026 um 00:05 Uhr)
IT NachrichtenLizenzprobleme: AnyDesk und TeamViewer(22.09.2026 um 00:30 Uhr)
Apple iOS & macOSApple's iOS 27.2 beta 2 reveals new anti-snatching protections(22.09.2026 um 00:27 Uhr)
AI & KI NachrichtenUC Irvine to Study AI for Writing Instruction(21.09.2026 um 23:31 Uhr)
AI & KI NachrichtenBurnham to call for global effort to control threats posed by AI(21.09.2026 um 23:30 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

The Python Bug That Bites Every Developer (Exactly Once)

I stared at my screen for 20 minutes, convinced Python was broken. My function was supposed to return a fresh list every time. Instead, it was accumulating items like it had a memory. The third call somehow contained results from the…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

I stared at my screen for 20 minutes, convinced Python was broken.



My function was supposed to return a fresh list every time. Instead, it was accumulating items like it had a memory. The third call somehow contained results from the first two.




def add_item(item, items=[]):
items.append(item)
return items

print(add_item("apple")) # ['apple'] ✓
print(add_item("banana")) # ['apple', 'banana'] ???
print(add_item("cherry")) # ['apple', 'banana', 'cherry'] !!!






I expected ['banana']. I got ['apple', 'banana'].



This is Python's mutable default argument bug — and every Python developer gets bitten by it exactly once.






The Mental Model That's Lying to You



When you see items=[], your brain reads: "If no list is provided, create an empty one."



That's wrong.



What Python actually does: "Create one empty list **right now, when the function is defined, and reuse that same object for every call."



The default value isn't "an empty list" — it's a reference to one specific list object that was created when Python first read your function.



Let me prove it:




def add_item(item, items=[]):
print(f"List id: {id(items)}")
items.append(item)
return items

add_item("a") # List id: 4399504832
add_item("b") # List id: 4399504832 ← Same object!
add_item("c") # List id: 4399504832 ← Still same!






Every call uses the exact same list object. When you append to it, that change persists.






The Fix: Use None as a Sentinel



The solution is elegant once you understand the problem:




def add_item(item, items=None):
if items is None:
items = [] # Create a NEW list each call
items.append(item)
return items

print(add_item("apple")) # ['apple']
print(add_item("banana")) # ['banana'] ✓






Now each call that doesn't provide items gets a fresh list, created at call time, not definition time.



This pattern should become automatic for any mutable default:




# Lists
def process(items=None):
if items is None:
items = []
...

# Dictionaries
def merge(config=None):
if config is None:
config = {}
...

# Sets
def collect(seen=None):
if seen is None:
seen = set()
...









Why This Actually Matters



This isn't just a gotcha for interviews. It causes real bugs:





  • Caching gone wrong: Your memoization function "remembers" too much


  • API handlers accumulating state: Each request sees data from previous requests


  • Test pollution: Tests pass individually but fail when run together



I've seen this bug in production code from senior engineers. It's subtle because the function works perfectly the first time — the bug only appears on subsequent calls.






The Deeper Lesson



Python's object model is consistent: variables are names pointing to objects. When you write items=[], you're creating an object and storing a reference to it. That reference is evaluated once — when the def statement runs.



Understanding this doesn't just help you avoid one bug. It unlocks how Python actually works under the hood.






This article is adapted from my upcoming book, **Zero to AI Engineer: Python Foundations.



I share excerpts like this on Substack — follow along for more!

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten The Python Bug That Bites Every Developer (Exactly Once)

Thematisch verwandte Begriffe: Python, That, Bites, Every · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-49449 | Joplin is an open source note-taking and to-do application that organise…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick