DEV Community

Cover image for The Secret Life of Python: The Myth of the Box
Aaron Rose
Aaron Rose

Posted on

The Secret Life of Python: The Myth of the Box

Rain lashed against the tall, arched windows of the library, blurring the lights of London into smears of amber and grey. Inside, the air smelled of old paper and fresh espresso.

Timothy sat at the heavy oak table, his brow furrowed. He was staring at a few lines of Python code as if they were a riddle written in an ancient tongue.

"It feels... reckless," Timothy murmured.

Margaret looked up from her book, adjusting her glasses. "What does?"

"Python," Timothy said, spinning his laptop around. "Look at this. I create a variable called x. I put the number 10 inside it. Then, two lines later, I shove the word 'Hello' into the same box. In C++, the compiler would scream at me. Here? It just... happens."

x = 10
print(x)

x = "Hello"
print(x)
Enter fullscreen mode Exit fullscreen mode

"It feels like there are no rules," Timothy continued. "Like I’m just throwing things into buckets without checking if they fit."

Margaret stood and walked over to the whiteboard she kept by the fireplace. She wiped away a diagram of a binary tree and picked up a marker.

"That is your mistake, Timothy," she said softly. "You are thinking of variables as boxes. That is a lie that other languages told you."

The Box Analogy

"In many statically typed languages like C," Margaret began, drawing a square on the board, "a variable is a container. It has a fixed shape and size. If you make an integer box named x, you can only put integers inside it. The box is the memory location."

"But Python," she said, drawing a large circle representing the computer's memory, "does not have boxes. It has Name Tags."

The Name Tag Reality

Margaret drew the number 10 floating in the memory circle. Then, she drew a tag with a string attached to it. On the tag, she wrote x. The string connected x to 10.

"When you type x = 10," Margaret explained, "Python creates an object representing the integer 10 somewhere in memory. Then, it takes a name tag labeled x and ties it to that object."

"Now," she continued, "when you write x = 'Hello', you aren't emptying a box and refilling it. You are simply untying the tag from the 10 and tying it to a new object, the string 'Hello'."

"The 10 is still there?" Timothy asked, blinking.

"For a moment, yes," Margaret replied. "Eventually, when no references remain, the Garbage Collector will sweep it away. But x never held the value. x only pointed to it."

The Evidence: Checking IDs

Timothy looked skeptical. "That’s a nice metaphor, Margaret. But how do we know that’s actually happening?"

Margaret smiled. "We ask Python to show us its ID cards. Every object has a unique address. If x were a box, the box's address wouldn't change just because we changed the contents. But watch."

She reached over Timothy’s shoulder and typed:

x = 10
print(f"x is 10. Address: {id(x)}")

x = "Hello"
print(f"x is Hello. Address: {id(x)}")
Enter fullscreen mode Exit fullscreen mode

She hit run.

x is 10. Address: 140704766276680
x is Hello. Address: 2336829285680
Enter fullscreen mode Exit fullscreen mode

Timothy’s eyes widened. "The address changed completely."

"Exactly," Margaret said. "The numbers themselves don't matter; what matters is that they are different. x isn't a place. It is a reference to an object. When you assigned it to 'Hello', you pointed that reference at a completely different neighborhood in memory."

The "Secret" Consequence

"This seems like a technicality," Timothy said. "Why does it matter to me?"

"Because of what happens when you introduce a second variable," Margaret warned. "This is where the 'Secret Life' trips up beginners."

She wrote a new snippet on the board.

a = [1, 2, 3]
b = a
b.append(4)

print(a)
Enter fullscreen mode Exit fullscreen mode

"If a was a box," Timothy reasoned, "and I copied its contents into b... then changing b shouldn't hurt a."

"If they were boxes, you would be right. But they are tags."

Margaret drew a list [1, 2, 3] in the memory cloud. She drew a tag a attached to it. Then, she drew a second tag, b, attached to the exact same list.

"You didn't copy the list," Margaret whispered dramatically. "You just added a second name tag to the same object."

Timothy hesitated, then ran the code.

[1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

"I changed a," Timothy gasped. "But I never touched a!"

"You touched the object that a was holding hands with," Margaret corrected. "This is the power and the danger of Python. We are trading expensive copying operations for efficient referencing. But you must always remember: You are not holding the data. You are holding a map to the data."

Margaret’s Cheat Sheet

Margaret tore a page from her notebook and slid it across the table to Timothy.

  • Variables are NOT Boxes: They are labels (references) attached to objects.
  • Assignment (=) is Tagging: x = 10 means "Put the tag x on the object 10."
  • Ids don't lie: Use id(variable) to see where the reference is actually pointing.
  • Aliasing: b = a means two tags on one object. Changing the object via b will surprise you when you look at a.

Timothy looked at the code again. It didn't look reckless anymore. It looked efficient.

"So," Timothy said, picking up his coffee. "If I really wanted to copy the list, I'd have to do something special?"

"Precisely," Margaret smiled, returning to her book. "If you want a true copy, you must ask for it explicitly using .copy(). But that is a story for another day. For now, just remember: in Python, the world is fluid. We just label the parts we want to keep."


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

Top comments (0)