Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungTCP vs UDP: The Two Ways to Move Data, and Why Neither Is "Better"(21.09.2026 um 09:31 Uhr)
Sichere ProgrammierungBukan Sekadar Variabel, Tapi Nyawa dari Aplikasi Kamu! 🚀(21.09.2026 um 09:36 Uhr)
Sichere ProgrammierungAI voice agent for customer service: what stops callers hanging up?(21.09.2026 um 09:42 Uhr)
Sichere ProgrammierungReading a small model's confidence instead of its prose(21.09.2026 um 09:47 Uhr)
Sichere ProgrammierungTCP vs UDP: The Two Ways to Move Data, and Why Neither Is "Better"(21.09.2026 um 09:31 Uhr)
Sichere ProgrammierungBukan Sekadar Variabel, Tapi Nyawa dari Aplikasi Kamu! 🚀(21.09.2026 um 09:36 Uhr)
Sichere ProgrammierungAI voice agent for customer service: what stops callers hanging up?(21.09.2026 um 09:42 Uhr)
Sichere ProgrammierungReading a small model's confidence instead of its prose(21.09.2026 um 09:47 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

The Secret Life of Python: The Phantom Copy

Why = doesn't actually copy your data in Python. Timothy stared at his screen, his face pale. "Margaret? I think I just accidentally deleted half the database." Margaret wheeled her chair over immediately, her voice calm. "Don't…

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

Why = doesn't actually copy your data in Python.






Timothy stared at his screen, his face pale. "Margaret? I think I just accidentally deleted half the database."



Margaret wheeled her chair over immediately, her voice calm. "Don't panic. Tell me exactly what happened."



"I was testing a script to clean up our user list," Timothy explained. "I wanted to test it safely, so I made a copy of the list first. I thought if I messed up the copy, the original would be safe."



He showed her the code:




# Timothy's Safety Plan

# The original list of critical users
users = ["Alice", "Bob", "Charlie", "Dave"]

# Create a "backup" copy to test on
test_group = users

# Timothy deletes 'Alice' from the test group
test_group.remove("Alice")

# Check the results
print(f"Test Group: {test_group}")
print(f"Original Users: {users}")







Timothy hit Run, hoping for a miracle.



Output:




Test Group: ['Bob', 'Charlie', 'Dave']
Original Users: ['Bob', 'Charlie', 'Dave']







Timothy slumped. "See? I removed Alice from the test_group, but she disappeared from the users list too! How is that possible? I touched the backup, not the original!"






The Address, Not the House



Margaret studied the code. "This is one of the most common misunderstandings in Python, Timothy. It comes down to how Python handles memory."



She grabbed a notepad. "When you wrote test_group = users, what did you think that command did?"



"I thought it created a new list," Timothy said. "I thought it took all the names from users and copied them into a new variable named test_group."



"That is a very reasonable assumption," Margaret said gently. "But Python takes a shortcut for efficiency. Copying data takes time and memory. So instead of copying the house, Python just copies the address."



She drew a simple diagram. On the left, she wrote the word users. On the right, she drew a box containing the list of names. She drew an arrow pointing from users to the box.



"When you wrote test_group = users, you didn't create a new box. You just gave the second variable the address of the original box."



Timothy looked at the diagram. "So users and test_group are just two different names for the exact same object?"



"Exactly," Margaret smiled. "It’s like having a shared document online. You gave me the link (the reference). If I delete a paragraph, it’s deleted for you too. We are both looking at the same document."






Breaking the Link



"So how do I actually make a copy?" Timothy asked. "I want a separate box."



"We have to be explicit," Margaret said. "We need to tell Python to take the data and build a new list."



She showed him the .copy() method.




# Margaret's Fix: Explicit Copying

users = ["Alice", "Bob", "Charlie", "Dave"]

# .copy() creates a brand new list with the same data
test_group = users.copy()

test_group.remove("Alice")

print(f"Test Group: {test_group}")
print(f"Original Users: {users}")







Output:




Test Group: ['Bob', 'Charlie', 'Dave']
Original Users: ['Alice', 'Bob', 'Charlie', 'Dave']







Timothy breathed a sigh of relief. "Alice is safe."



"She is," Margaret confirmed. "By using .copy(), you forced Python to create a second, independent list in memory. Now, test_group has its own box, and changes there don't touch the original."



She added a small warning note. "Just remember, .copy() makes a 'Shallow Copy.' If your list has other lists inside it, those inner lists are still shared. But for a simple list of names like this, it is exactly what you need."






Margaret’s Cheat Sheet



Margaret opened her notebook to the "Memory Management" section.



The Trap: Assuming that new_list = old_list creates a copy.

The Reality: In Python, assignment (=) creates a Reference (a nickname), not a copy. Both variables point to the same object.

The Why: Python does this to save memory and speed.

The Fix:





  • Shallow Copy: new_list = old_list.copy() (Standard way).


  • Slicing: new_list = old_list[:] (Older, but common way).



The Check: You can verify if two variables are the same object using id(a) == id(b).



Timothy made a note in his editor. "I'll never assume = means 'copy' again."



"It's a rite of passage," Margaret assured him. "Every Python developer learns this lesson the hard way. Better to learn it on a test script than on the production database."






In the next episode, Margaret and Timothy will face "The Safety Net"—where Timothy learns how to catch errors gracefully so his programs don't crash when users make mistakes.






Aaron Rose is a software engineer and technology writer at tech-reader.blog and the author of Think Like a Genius.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten The Secret Life of Python: The Phantom Copy

Thematisch verwandte Begriffe: Secret, Life, Python, Phantom · 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-94030 | A security vulnerability has been detected in SerenityOS up to 3d83e4509…
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