Scope Inspector¶
Run the Scope Inspector MicroSim Fullscreen
Edit in the p5.js Editor
About This MicroSim¶
Scope is the most abstract idea in the first half of the book: variables
"living" in invisible rooms. This inspector draws the rooms. The Global room
is always on screen; each function call opens a temporary Function room that
vanishes at return — taking its variables with it. Every name lookup
highlights the box Python actually uses, and when a local lookup misses, a
badge says "not found here → check the Global room." The Shadowing Bug preset
shows a local score = 99 hiding the global 10; The global Fix preset shows
the global keyword writing straight into the Global room.
Learning objective: The student will be able to determine which variable a name refers to at any line, differentiating local, global, and shadowed variables.
- Bloom's Taxonomy (2001): Analyze — differentiate, attribute, debug
- Interaction pattern: step-through with nested scope rooms and lookup highlighting
How to Use¶
- Start with Read the Global: watch the empty Function room appear, and the lookup fall through to the Global room.
- Switch to Shadowing Bug and predict: what does the final
print(score)show? Step through — the local 99 box is thrown away with its room. - Run The global Fix to see how the
globalkeyword changes where the write lands.
Iframe Embed Code¶
You can add this MicroSim to any web page by adding this to your HTML:
1 2 3 4 | |
Lesson Plan¶
Grade Level¶
Upper elementary and middle school (ages 10-14), Chapter 11 (Functions, Parameters, and Scope)
Duration¶
15 minutes
Prerequisites¶
- Defining and calling functions (Chapter 4)
- Variables and assignment (Chapter 3)
Activities¶
- Rooms tour (4 min): Step through Read the Global. Ask: "when does the Function room exist, and when does it not?"
- Predict the bug (5 min): Before stepping Shadowing Bug, students vote: 99 or 10? Step through and identify the exact step where the local box is destroyed.
- Fix comparison (4 min): Run The global Fix and have students state in
one sentence what
global scorechanges.
Assessment¶
- Student predicts the output of a shadowing program correctly
- Student can explain why a local variable disappears after the call
- Student can state when the
globalkeyword is needed and why it is rare
References¶
- Python FAQ — What are the rules for local and global variables? — official explanation
- p5.js — the JavaScript library used to build this MicroSim