Programming

In Python, a name is a sticker, not a box

Two names can sit on one object. Whether it can change is what makes lists and strings behave differently.

What you'll learn

  • Why b = a is a second name, not a copy
  • Which Python types change in place and which are replaced
  • How the mutable default argument bug gets written
Python 8 steps 24 seconds views

Was this useful?

Share

Understand it one step at a time

The short runs these in order in about 24 seconds. Here they are written out — pick any step to jump the short straight to it.

1 Step 1 of 8

You changed b, a changed too

A list assigned to a second name with b = a is not a copy at all. Both variable names simply point to the exact same object in memory.

Frequently asked questions

When you write b = a in Python, does it copy the object?
No — it gives the same object a second name. Both a and b point at the same underlying object, so if that object is mutable, a change made through either name is visible through both.
What is the classic mutable-default-argument bug?
Defining a function with a mutable default, like def f(items=[]), creates that list object once, at function-definition time — not on every call. Every call that doesn't pass its own list shares and mutates the same default list across calls.

Free app · no app store

These are built for a phone

Every short is drawn at full portrait height, the shape a phone already is. Installed, it opens full-bleed with no address bar across the top — and the whole library reads offline.

How it works

More shorts

Get new posts by email

New technical articles, Azure AI and GitHub Copilot updates, and upcoming events. No spam, unsubscribe anytime.

navigate open