Python traps 2 · Chapter 3 of 16
A local name that hides a global
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Names
- 3 tasks
- Python
- In the all labs plan
The first 2 chapters of Python traps 2 are free. This one comes with the all labs plan.
Why you need this
global SPEED stops the crash, because now the function reads and writes the module's SPEED. That is exactly the trouble:
15
25
25
12
Those are eta_minutes(5), then with traffic, then without again, and then SPEED itself. One slow trip changed the module's speed, so every later trip is slow too. The fix is a local name of its own for this call's value: speed = SLOW_SPEED if traffic else SPEED, which prints 15, 25, 15 and leaves SPEED at 20.
global (a module name) and nonlocal (a name in the enclosing function) are right when you mean to change shared state, such as a counter a closure keeps:
def make_counter():
count = 0
def bump():
nonlocal count
count += 1
return count
return bump
bump = make_counter()
bump()
print(bump())
2What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.py: a dry-day fare that crashes
Write reproduce() in repro.py. Work out the fare of a dry 4 km trip, rider_fare(4) from fares.py, and return it. If it raises UnboundLocalError, catch it and return the error's message, str(error), instead.
The check runs your reproduce() twice: beside the original fares.py it must return "cannot access local variable 'PER_KM' where it is not associated with a value", and beside a fixed fares.py it must return 40 (20 plus 5 a km for 4 km). So it must really call rider_fare for a dry trip: a repro that just returns the message fails the second run. No real waiting and no input(): reproduce() must finish within 3 seconds.
- Task 2
fares.py: dry days pay again
Fix rider_fare in fares.py. rider_fare(km) pays BASE + PER_KM * km and rider_fare(km, raining=True) pays BASE + SURGE_PER_KM * km, in any order: a dry fare worked out after a rainy one is still a dry fare. The module's BASE, PER_KM and SURGE_PER_KM stay 20, 5 and 7 however many fares are worked out. day_payout(rider, trips) adds up the rider's fares (each trip a dict with "km" and "raining") and returns the total.
Keep the log line, payout: <rider> <n> trips <total>, printed once per day_payout. The check imports a fresh copy of your fares.py, works out rainy and dry fares in mixed orders and reads PER_KM after each: a fix that stops the crash but pays a dry trip at the surge rate after a rainy one is as wrong as the bug.
- Task 3
test_fares.py: a test that would have caught it
Write a pytest file, test_fares.py, that would have caught this bug before Saturday's payout. The check runs your file three times: beside the original fares.py, where at least one test must fail; beside the fixed fares.py, where every test must pass; and beside a wrong fix that adds global PER_KM, where at least one test must fail.
So test both sides: a dry fare works at all, and a dry fare worked out after a rainy one, in the same test, is still paid at PER_KM. Your tests share one copy of fares.py, so what one call changes, the next call sees. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.py: a dry-day fare that crashes
- fares.py: dry days pay again
- test_fares.py: a test that would have caught it
More in Names
Build "A local name that hides a global" yourself
Monsoon season at DabbaDrop brings a new pile of tickets from Kunal. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Python traps 2 are free. This one comes with the all labs plan.