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
Share this passage
Drawing…
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.
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.
More shorts
-
Azure
Secure a Mobile API in 5 Steps
8 steps · 25s
-
Azure
What Happens When You Tap Sign In
8 steps · 25s
-
Azure
Your Mobile App Is Leaking Its API Key
8 steps · 26s
Get new posts by email
New technical articles, Azure AI and GitHub Copilot updates, and upcoming events. No spam, unsubscribe anytime.