Python, properly · Chapter 15 of 42
Scope and closures
By the end of this chapter you know exactly where Python looks for a name (LEGB), why assigning to an outside name inside a function fails, how to pass functions around like any other value, write a lambda, and build functions that remember things: closures. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Data and functions
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.
Why you need this
DabbaDrop needs order ids that count up, D0001, D0002, without two counters ever clashing. Rafiq wants coupons that each work their own way, picked by their code. Bhavna wants the menu sorted by price, not by name. All three need functions that are values: made by other functions, kept in a dictionary, handed to sorted.
Chapter 13 said "names inside a function are local". That is half the story, and the other half is where most confusing Python bugs come from: a function that cannot see a name you are sure exists, or a whole list of functions that all give the same answer. Knowing the rule turns those into one-minute fixes.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
order_ids.py: ids that count up
Write make_id_maker(prefix): it returns a new function. Each call of that function returns the next id: the prefix and a number counting from 1, written with four digits. For example, with next_id = make_id_maker("D"), next_id() returns D0001, then D0002, then D0003.
Two makers count separately: make_id_maker("R") gives R0001 even after the D maker has given out three ids. Use no global variable.
- Task 2
sort_menu.py: cheapest first
Write cheapest_first(dishes): dishes is a list of dictionaries with name and price. Return a new list of the names only, from the cheapest dish to the dearest; dishes with the same price go in alphabetical order of their name. For example, for Chole 150, Roti 15, Aloo Gobi 150 and Lassi 60 it returns ['Roti', 'Lassi', 'Aloo Gobi', 'Chole'].
Do not change the list you are given.
- Task 3
coupons.py: a dictionary of functions
Rafiq's coupons each work differently. Make a dictionary called COUPONS mapping each code to a function that takes a total and returns the new total: DABBA10 takes 10% off (the discount rounded down to whole rupees, so 245 becomes 221), and FIRST50 takes Rs 50 off, but never below 0.
Then write apply_coupon(total, code) that looks the code up in COUPONS and calls its function, and returns the total unchanged for an unknown or empty code.
- Task 4 · interview question
Interview: surcharges.py, functions made in a loop
make_surcharges(amounts) should return one function per amount, each adding its own amount to a bill. For [10, 20, 30], the three functions given 100 should return 110, 120 and 130. Right now they all return 130.
Fix make_surcharges so each function keeps its own amount. Keep it returning a list of functions, one per amount, in order.
What you will learn
- LEGB: Local, Enclosing, Global, Built-in, the order Python searches for a name; and the trap of hiding a built-in.
- global and nonlocal: changing a name outside the function, and why UnboundLocalError happens.
- Functions as values: assigning them, sorted(key=...), lambda.
- Closures: inner functions that remember their surroundings, and when they look a value up.
More in Data and functions
Build "Scope and closures" yourself
DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. 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, properly are free. This one comes with the all labs plan.