The Secret Life of Python: The Statue in the Memory

 

The Secret Life of Python: The Statue in the Memory




The afternoon sun cut through the dust motes dancing in the library air. Timothy was feeling confident. He had mastered the "Name Tag" concept from their previous session. He felt like he finally understood the fluid nature of Python.

He decided to demonstrate his newfound knowledge to Margaret.

"Watch this," Timothy said, turning his laptop toward her. "I’m going to use the aliasing trick we talked about. I’ll make two tags, greeting_one and greeting_two. They point to the same string object. Then I’ll modify one, and prove that the other changes too."

Margaret lowered her tea cup, an unreadable expression on her face. "Proceed," she said.

Timothy typed rapidly.

greeting_one = "Hello"
greeting_two = greeting_one

# Timothy's Flawed Plan: "Modify the object via the second tag."
greeting_two += " World"

print(f"greeting_two is: {greeting_two}")
print(f"greeting_one is: {greeting_one}")

"In my mind," Timothy narrated, "Since greeting_two is just a reference to the same object, greeting_one should now also say 'Hello World'."

He hit Enter.

greeting_two is: Hello World
greeting_one is: Hello

Timothy froze. He ran it again. Same result.

"But..." He looked at Margaret, betrayed. "You said they were references! You said if I changed the object via one tag, the other would see the change. You showed me with the list!"

"I did," Margaret nodded.

"Then why did this one split? Why is greeting_one still just 'Hello'?"

The Mutable vs. The Immutable

Margaret stood up and walked to the marble bust of a Roman senator that stood in the corner of the room. She patted the statue’s cold, stone head.

"Timothy, tell me. If I wanted to give this senator a mustache, could I just mold the stone?"

"No," Timothy said. "It’s stone. You’d have to chip it away, or... well, you can't change it without breaking it."

"Exactly," Margaret said. "Strings in Python are like this statue. They are Immutable. They cannot be changed. Not ever."

She returned to the whiteboard. She drew the "Name Tag" diagram again.

  1. She drew a stone block labeled "Hello".
  2. She drew two tags, greeting_one and greeting_two, both tied to that stone block.

"When you wrote greeting_two += ' World'," Margaret explained, "Python looked at the stone block "Hello". It knew it couldn't carve ' World' onto that stone. It is forbidden."

She drew a new process on the board:

  1. Python takes the value "Hello" from the original block.
  2. It grabs the new value " World".
  3. It creates a brand new stone block in a new memory location. It carves "Hello World" onto this new block.
  4. It detaches the greeting_two tag and ties it to the new block.

"The old block," Margaret said, pointing to "Hello", "was never touched. It sits there, perfect and unchanged. greeting_one is still holding onto it. greeting_two simply moved on."

The List Contrast

"But the list..." Timothy stammered.

"A list is a whiteboard," Margaret countered. "It is Mutable. You can write on it, erase it, and add to it without buying a new whiteboard. That is why the changes showed up for both tags. But a String? A String is a diamond. If you want a different shape, you must get a different diamond."

The Identity Proof

"I don't believe it," Timothy muttered. "I want to see the IDs."

"A wise instinct," Margaret smiled. "Check the addresses before and after the change."

Timothy typed out the proof.

s = "Hello"
print(f"Start ID: {id(s)}")

s += " World"
print(f"End ID:   {id(s)}")

Start ID: 2667049832100
End ID:   2667051284300

The specific ID numbers don't matter; what matters is that they are different.

"Different addresses," Timothy admitted. "The numbers are totally different."

"So, every time I modify a string... I'm actually creating a clone?"

"Every single time," Margaret confirmed. "Uppercasing, replacing, slicing, adding. None of them change the original. They all manufacture a new object and return a reference to it."

Margaret’s Cheat Sheet

Margaret opened her notebook to a fresh page. "This is the duality of Python. You must know which objects are stone and which are whiteboards."

  • Immutable (Stone): Strings, Integers, Floats, Tuples.
  • You cannot change them in place.
  • Operations like += create new objects (Rebinding).
  • Safe to share references between parts of your code.

  • Mutable (Whiteboard): Lists, Dictionaries, Sets.

  • You change them in place.

  • Operations modify the original object.

  • Sharing references means sharing changes (Aliasing).

"So," Timothy leaned back, looking at the code with new eyes. "When I see s = s.upper(), I'm not shouting at the old string."

"No," Margaret replied, taking a sip of her tea. "You are creating a new shout, and letting the whisper fade away."


Next time, Margaret and Timothy will explore "The Secret Life of Truth"—where they discover that in Python, even emptiness can be False.


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

Comments

Popular posts from this blog

The New ChatGPT Reason Feature: What It Is and Why You Should Use It

Insight: The Great Minimal OS Showdown—DietPi vs Raspberry Pi OS Lite

Raspberry Pi Connect vs. RealVNC: A Comprehensive Comparison