Best afterAliasing: two names, one list
Sorting into two piles
Walk down a row of numbers and drop each one onto the evens pile or the odds pile. Easy — until the way you make the two piles quietly makes them the same pile. Step the buggy version and watch every number land on both piles at once.
Sort the numbers into two piles: evens in one pile, odds in the other.
6 else:nums noteOur numbers: 3, 8, 5, 2. We want evens in one pile and odds in the other.
Click a line, drag the slider, or use the ← → keys.
What you are looking at
Four numbers — 3, 8, 5, 2 — and two piles. Each number is even or odd, so it belongs in exactly one pile. The loop visits them one at a time (the current number is n) and appends it to the right pile.
- Buggy — the setup is
evens = odds = []. It looks like two empty piles, but it creates one list with two names. Step it: the first append shows the number landing on both piles, because they are the same object. By the end, both piles hold every number — the partition never happened. CPython shows the aliasing; we don't have to claim it. - Procedural — make two genuinely separate lists (
evens = []thenodds = []). Now each append touches only its own pile, and the numbers split cleanly: evens 8, 2 and odds 3, 5.
Try clicking the for line repeatedly to walk one iteration at a time. Every pile you see came from running the program under CPython's tracer — see how GlassBox stays honest.