Lesson 9 of 21 · 52 min · UG / professional
Scopes: locals, globals, LEGB
A parameter named rate does not rewrite CONFIG unless you pass CONFIG. Locals die at return. global is a last resort. LEGB is the search order on a laptop.
Lab recipe cards
Functions are reusable procedures.
You will be able to
- ✓ Pass what a helper needs; do not reach for a global
- ✓ Know that a parameter is local to the call (LEGB is the laptop search order)
- ✓ Keep CONFIG as data you pass, not as a hidden dependency
A parameter named rate does not rewrite CONFIG
late_fee(days) should not peek at a file-level GST just because the name is nearby. Name lookup is how large programs stay sane: the function uses what you passed, plus names it assigned inside the body. Those inner names vanish when the call returns. That is why a test can call add_gst(100, 0) without standing up a campus-wide CONFIG first.
On a laptop, Python searches LEGB: local, enclosing (a nested def), global (file level), then built-in (len, print). You do not need the acronym to use the rule: if the helper needs gst, pass gst. If fifty functions read CONFIG, pass CONFIG into the few that are allowed to. Hidden globals make internships look like they work until the second college copies the file.
Locals do not leak; global is a last resort
Inside net_pay you may assign cut = 12. After the call, cut should not appear in the caller unless you returned it. If you need two results, return a dict with two keys rather than setting globals. That record is testable.
The global keyword on a laptop lets a function assign to a file-level name. It is how PF rates become untestable. Pass CONFIG, return a number, print at the edge.
Laptop: LEGB — enclosing vs global
GST = 18
def bill(amount):
def add_on(base):
return base * (100 + GST) / 100
return add_on(amount)
print(bill(100))
Words that matter
- Local
- A name that lives for one function call.
- Global
- A name at file level — easy to reach, hard to test.
- LEGB
- Laptop search order: local, enclosing, global, built-in.
- Explicit argument
- A value the caller handed in — the helper’s contract.
Common mistakes
Avoid: A helper that reads GST from a global and also prints a receipt.
Do this: Pass rate in; return a number; print at the edge.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — pass the rate
The function never mentions CONFIG. Tests can pass 0.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — a local cut does not replace CONFIG
cut is inside the function. CONFIG still has gst 18 after the call.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Example program — Pass the whole config when many fields are allowed
Still explicit: the helper lists config as a parameter.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — No hidden rate
tax(amount, rate) returns amount * rate / 100. Print tax(200, 18) so 36 appears.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.