Python traps 2 · Chapter 12 of 16
A cached value changed by its caller
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Libraries
- 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.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.py: Monday's offer on Tuesday's menu
Write reproduce() in repro.py. Call price_list(10) from menu.py, as the Monday lunch offer did, then price_list(0), as every customer's app does after it, and return the price of "paneer-butter-masala" in the second list, as an int.
The check runs your reproduce() twice, each time in a fresh python3: beside the original menu.py it must return 198 (the offer stayed in the menu), and beside a fixed menu.py it must return 220. So it must really ask for both price lists, the offer first. No real waiting and no input(): reproduce() must finish within 3 seconds.
- Task 2
menu.py: an offer for one, not a new menu for all
Fix price_list(discount_pct=0) in menu.py. It gives a list of {"id": dish, "price": rupees}, one per dish in menu order, each at the menu price less discount_pct percent, in whole rupees, rounded down (paneer at 10% off is 198), however many discounts were asked for before. A caller may change or clear the list it is given, and its dicts, without changing any later price list.
The menu must still be read once: keep load_menu and its cache, and keep read_menu, MENU and the dish order. The check imports a fresh copy of your menu.py, replaces read_menu with its own menu that counts its calls, asks for many discounts in a row, changes the lists it is given, and checks the menu was read once. Copying the list is not enough: the dishes in a copied list are the same dicts.
- Task 3
test_menu.py: a test that would have caught it
Write a pytest file, test_menu.py, that would have caught this bug before the offer went out. The check runs your file three times: beside the original menu.py, where at least one test must fail; beside the fixed menu.py, where every test must pass; and beside a wrong fix that copies the list with list(load_menu()) but changes the same dish dicts, where at least one test must fail.
So test both sides: a discounted price list has the offer's prices, and a plain price list asked for after it, in the same test, has the menu prices again. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.py: Monday's offer on Tuesday's menu
- menu.py: an offer for one, not a new menu for all
- test_menu.py: a test that would have caught it
More in Libraries
Build "A cached value changed by its caller" 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.