1.2.31 · D1Introduction to Programming (Python)

Foundations — global and nonlocal keywords

1,682 words8 min readBack to topic

Before you can trust a single line of the parent note, you need eight small ideas built from nothing. We go one at a time: plain words → the picture → why the topic needs it. Nothing appears before it is defined.


1. A variable — a labelled box

The picture: think of a sticky label on a box. The label is count, the thing inside is 0.

Figure — global and nonlocal keywords

Why the topic needs it: the entire parent note is about which box a name points to when there are several boxes with the same label in different places. If you don't picture the box-and-label, "rebinding" and "scope" have nothing to hang on.


2. Reading vs assigning — two different actions

This is the single most important distinction on the whole page.

The picture: reading = peeking into a box. Assigning = ripping the label off and sticking it on a different box.

Figure — global and nonlocal keywords

Why the topic needs it: the parent's core rule is "assigning a name makes it local, reading does not." Without separating these two actions the rule is meaningless.


3. Scope — the room a name lives in

The picture: rooms inside rooms. The whole file is the front hall (the module). Each function you enter is a smaller room drawn inside it.

Figure — global and nonlocal keywords

Why the topic needs it: global and nonlocal are instructions about which room a label belongs to. No rooms → nothing for these keywords to point at. See Scope and LEGB rule for the full treatment.


4. A function — a room you can enter and leave

The picture: the middle room in the figure above. You walk in, do work with labels only that room can see, and walk out.

Why the topic needs it: locality is defined per function. "This name is local" always means "local to this function's room." Prerequisite: Functions in Python.


5. Module level — the front hall (the Ground floor)

The picture: the outermost box in the rooms figure — the one labelled module / front hall.

Why the topic needs it: global x means exactly "use the label x that lives here, in the front hall." The mnemonic G = Ground floor on the parent page is this room.


6. Nested function and enclosing scope — a room inside a room

The picture: a small room drawn inside the middle room. The inner room can see out into the middle room (its enclosing scope) and further out into the front hall.

Figure — global and nonlocal keywords

Why the topic needs it: nonlocal targets this room — the enclosing function, not the front hall. The mnemonic N = Next door is this middle room. This is also the machinery behind Closures.


7. The LEGB ladder — the order Python searches when reading

The picture: climbing outward through the nested boxes — start in your own room (L), step out to the enclosing function (E), then the module front hall (G), then Python's own supplied names like print (B).

Why the topic needs it: reading uses this ladder automatically (that's why print(count) works with no keyword). Assigning does not climb the ladder — it stays Local unless you override with global/nonlocal. Understanding LEGB is what makes that asymmetry click.


8. Rebinding a name vs mutating an object

A subtle trap the parent page warns about — worth its own picture.

The picture: rebinding = the label jumps to a new box. Mutating = the same box gets a new item dropped inside; the label never moves.

Why the topic needs it: half the confusion around global comes from mixing these up. See Mutable vs Immutable objects.


9. UnboundLocalError — the symptom this all explains

The picture: you reached for the local box, but it's empty — you tried to read it before you filled it, and Python refuses to peek outside because assigning made it local. Full detail: UnboundLocalError.

Why the topic needs it: this is the "why does this crash?" classic the parent opens with — it is a direct consequence of the assignment rule, not a random bug.


Prerequisite map

Variable = name on a box

Reading vs Assigning

Scope = a room of names

Function = a room you enter

Module level = front hall

Nested function = room in a room

LEGB reading ladder

Rebind vs mutate object

UnboundLocalError

global and nonlocal keywords


Equipment checklist

Test yourself — reveal only after you have answered out loud.

What does count = 0 create, in box-and-label terms?
A box holding 0 with the label count stuck on it.
Which single symbol tells you an action is assigning rather than reading?
The = with the name on its left (also +=, -=, etc.).
Does print(count) read or assign count?
It reads — no =, name only being looked at.
What is a scope?
A region (a "room") with its own set of name labels; each function call gets a fresh one.
Where do names defined at the top of the file (outside any function) live?
In the module / global scope — the "front hall".
What is an enclosing scope?
The outer function's room, when one function is defined inside another.
Spell out LEGB and say what it controls.
Local, Enclosing, Global, Built-in — the order Python searches when reading a name.
Does reading an outer name climb the LEGB ladder automatically?
Yes — reading needs no keyword; only assigning stays local by default.
What is the difference between rebinding a name and mutating an object?
Rebinding moves the label to a new value (x = ...); mutating changes the contents in place (x.append(...)) without moving the label.
Does mylist.append(5) on a global list need global?
No — it mutates the object, it does not rebind the name.
What causes an UnboundLocalError?
Reading a name that is local (because it's assigned somewhere in the function) before it has been given a value.
global binds at which level, nonlocal at which?
global → module (front hall); nonlocal → nearest enclosing function (next-door room).