Best afterValues and names

Swapping two values

Swapping two variables looks trivial until you try the obvious thing and a value vanishes. Step through each version below and watch the state change, line by line. Toggle between the two cups and the raw variables — they are the same thing at two zoom levels.

Cup a holds juice and cup b holds milk. Swap them, so cup a ends with milk and cup b ends with juice.

Swapping two cups — the tempting wrong way execution-derived · CPython
same data, two zoom levels
state after line 1 runs
Cup a a juice
Cup b b

noteCup a (the left cup) now holds juice.

step 1 / 4

Click a line, drag the slider, or use the keys.

What you are looking at

Two cups: cup a holds juice, cup b holds milk. We want them swapped. The hard part is that pouring one cup into the other overwrites what was already there.

Every state you see came from running the program under CPython's tracer at build time. The cups and the wording are human pedagogy laid on top — see how GlassBox stays honest.