Values and names
A variable is a name pointing at a value, not a box that remembers everything you ever put in it. Rebind the name and the old value is gone — unless you saved it somewhere first. Step through both versions and watch the names move.
price starts at 5, then becomes 7. Add the original price and the new price together (we want 5 + 7 = 12).
Reusing a name — the old value is gone execution-derived · CPython
variables
state after line 1 runs
| variable | value |
|---|---|
price | 5 |
step 1 / 3
Click a line, drag the slider, or use the ← → keys.
What you are looking at
Keep your eye on price: one name, one value at a time.
- Buggy —
price = 5thenprice = 7— the second line doesn't add a value, it replaces the one the name points at. Soanswer = price + priceis7 + 7 = 14, never5 + 7. The original 5 was never kept. - Procedural — Copy the value into
oldbefore reassigningprice. Now both 5 and 7 survive under different names, andansweris the12we wanted.
Every state you see came from running the program under CPython's tracer at build time — see how GlassBox stays honest.