Python, properly · Chapter 14 of 42
Arguments
By the end of this chapter you can give parameters default values, pass arguments by name, accept any number of them with args and *kwargs, force a caller to name an argument, and you know the mutable-default trap and why it happens. 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
- 5 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
A function that prices an order takes a subtotal and a delivery fee, every time. But most orders pay the standard fee, so every caller types the same 30. Then Rafiq wants an optional discount, Bhavna wants to tag orders with whatever details the kitchen needs today, and a refund must never go out without a reason written down.
With what you know, each new option is a new required parameter, and every existing call breaks. Python's arguments are much more flexible than "one value per parameter, in order": that flexibility is how print(..., sep=, end=) works, and every library you will use relies on it.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
order_price.py: defaults for the usual case
Write order_price(subtotal, delivery=30, discount=0): it returns subtotal plus delivery minus discount. The delivery fee is 30 and the discount 0 unless the caller says otherwise.
The check calls it in several ways: order_price(300) returns 330, order_price(300, 0) returns 300, order_price(300, discount=50) returns 280, and order_price(discount=20, subtotal=200, delivery=0) returns 180.
- Task 2
total.py: any number of amounts
Write total(*amounts, discount=0): it returns the sum of all the amounts it is given, minus discount. With no amounts the sum is 0.
For example total(140, 15, 90) returns 245, total() returns 0, total(100, 50, discount=20) returns 130, and total(*[60, 60]) returns 120. The discount can only be given by name.
- Task 3
tag.py: whatever details the kitchen needs
Write tag(order_id, **details): it returns the order id followed by each detail as name=value, in the order they were passed, all separated by a space, a bar and a space. With no details it returns just the id.
For example tag("D0042", area="Bandra", rider="R2") returns D0042 | area=Bandra | rider=R2, and tag("D0042") returns D0042.
- Task 4
refund.py: no refund without a reason
Rafiq wants every refund to say why, in so many words. Write refund(amount, *, reason): it returns Refund Rs, the amount, a colon and the reason. For example refund(240, reason="late delivery") returns Refund Rs 240: late delivery.
reason must be keyword-only and required: refund(240, "late delivery") and refund(240) must both stop with a TypeError, which Python raises for you once the def line is right.
- Task 5 · interview question
Interview: tally.py, the counter that never resets
tally(items, counts) counts each dish in a list into the dictionary counts and returns it. It has the mutable-default bug: tally(["roti", "roti"]) returns {'roti': 2}, but a second call tally(["dal"]) returns {'roti': 2, 'dal': 1}.
Fix it so that every call without counts starts from a new, empty dictionary, while a call given a dictionary still adds to that very dictionary and returns it: with counts = {"roti": 1}, tally(["roti"], counts) returns counts, which is now {'roti': 2}.
What you will learn
- Defaults and keyword arguments: optional parameters, and naming an argument in the call.
- The mutable-default trap: why a list default remembers old calls.
- args and kwargs*: any number of positional or named arguments, and and * in a call.
- Keyword-only parameters: arguments the caller must name.
More in Data and functions
Build "Arguments" 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.