Understanding Python aliasing, references, and the trap of shallow copies.
The winter wind rattled the library windows, but inside, Timothy felt secure. He was working on a critical task: the guest list for the annual Founders' Dinner.
"I am taking no chances," Timothy announced, tapping his screen. "I have my master_list of VIPs. But I need to add some tentative guests to see how it looks. So, I made a copy."
Margaret looked up from her book, her interest piqued. "A copy, you say?"
"Exactly," Timothy said confidently. "I assigned the master list to a new variable called draft_list. Now I can mess around with the draft without touching the original."
He showed her his code.
# Timothy's "Safety" Plan
master_list = ["Alice", "Bob", "Carol"]
draft_list = master_list
# Let's add a tentative guest to the draft
draft_list.append("Dave")
# Check the lists
print(f"Draft List: {draft_list}")
print(f"Master List: {master_list}")
"See?" Timothy hovered his finger over the Run button. "I'm adding 'Dave' to the draft. The Master List will stay safe and sound with just Alice, Bob, and Carol."
"Timothy," Margaret warned, "you are standing in front of a mirror, expecting your reflection to move independently."
Timothy hit Run.
# Console Output:
Draft List: ['Alice', 'Bob', 'Carol', 'Dave']
Master List: ['Alice', 'Bob', 'Carol', 'Dave']
Timothy gasped. "What? I didn't touch the Master List! I only appended to the draft!"
"Look at the Master List," Margaret pointed. "Dave is there. You have corrupted the holy record."
The Curse of the Alias
Timothy looked at the screen as if it were haunted. "But I made a new variable! draft_list = master_list. That creates a new list, doesn't it?"
"No," Margaret said. She walked to the whiteboard and drew a single, large rectangle. Inside, she wrote ["Alice", "Bob", "Carol"].
"This is the object," she said. "The list itself."
Then she drew a tag with a string attached to the box. The tag read master_list.
"When you wrote draft_list = master_list," Margaret explained, "you did not copy the box. You simply wrote a second name tag and tied it to the same box."
She drew the second tag, draft_list, attached to the exact same rectangle.
"You have two names for one object," she said. "We call this Aliasing. If you put 'Dave' in the box using the draft_list tag, and then look inside using the master_list tag, Dave is there. Because there is only one box."
Breaking the Mirror
"So how do I actually copy it?" Timothy asked, desperate to fix his VIP list. "I want a real clone. A separate box."
"You must ask Python to build a new container," Margaret said. "You cannot just copy the name; you must request a Slice."
She deleted his assignment and typed the correction.
# Margaret's Real Copy
master_list = ["Alice", "Bob", "Carol"]
# The Slice Syntax [:] creates a full, shallow copy
draft_list = master_list[:]
draft_list.append("Dave")
print(f"Draft List: {draft_list}")
print(f"Master List: {master_list}")
Timothy ran it again, holding his breath.
# Console Output:
Draft List: ['Alice', 'Bob', 'Carol', 'Dave']
Master List: ['Alice', 'Bob', 'Carol']
"It worked!" Timothy cheered. "The Master List is safe."
"The syntax [:] tells Python to take a slice of the list from the very beginning to the very end," Margaret explained. "When you slice a list, Python creates a brand new list object and fills it with the items from the old one. Now, you have two boxes."
She paused, a serious look crossing her face. "But be warned, Timothy. This is what we call a Shallow Copy. For a simple list of names, it is perfect. But be wary, for some lists are not so simple..."
Margaret’s Cheat Sheet
Margaret opened her notebook to the "Memory" section, right next to the entry on Strings.
-
Assignment (
b = a): Does NOT copy the object. It creates a new reference (alias) to the same object. Changes to one affect the other. - The Trap: Mutable objects (Lists, Dictionaries) are most dangerous here.
- The Fix (Shallow Copy):
-
Slicing:
b = a[:](The classic, fast way). -
Method:
b = a.copy()(The readable, modern way). Function:
b = list(a)(The constructor way).The Rule: "If you want to change it without consequences, you must clone it first."
Timothy looked at his two separate lists, finally relaxing. "I guess I learned something important," he murmured.
"Indeed," Margaret smiled, closing the ledger. "A name is not the thing itself. And a map is not the territory."
In the next episode, Margaret and Timothy will face "The Matryoshka Trap"—where they discover that even a copy might still be connected to the original, deep down inside.
Aaron Rose is a software engineer and technology writer at tech-reader.blog and the author of Think Like a Genius.
Top comments (0)