IntermediatePython · 31 min · 19 challenges

Functional Programming: Intermediate

Picture a Friday evening at Uber: a pricing job builds a fare function for each city in a loop, with Chicago at 1.2x surge, Boston at 1.5x and Austin at 1.8x. The audit report shows riders in all 3 cities charged 1.8x. Nothing crashed, and every line looks right. Whether riders are billed correctly comes down to when a closure reads its variable.

Folding a Sequence With reduce

Daily Life
Interviews

functools.reduce takes a function of 2 arguments and a sequence, and folds the sequence into a single value. It calls the function on the first 2 items, calls it again on that result and the third item, and keeps going left to right until the items run out. The function's first parameter is the running result, called the accumulator. The second parameter is the next item.

Watch it fold 4 batch row counts. The step function prints every call, so you can follow the accumulator from call to call.
from functools import reduce
def add_rows(running, batch):
total = running + batch
print(f"{running} + {batch} -> {total}")
return total
batches = [120, 45, 300, 15]
total = reduce(add_rows, batches)
print("total:", total)
total=
batches
[
]
0123
output0 lines
imports reduce
1/18

With no starting value, the first batch becomes the first accumulator, so 4 items make 3 calls.

Giving reduce a starting value

A third argument, the initializer, becomes the first accumulator, and the step then runs once per item: 4 batches make 4 calls. The initializer matters in 2 situations. The first is a sequence that can be empty. With no initializer and no items, reduce has nothing to return and raises TypeError: reduce() of empty sequence with no initial value, while reduce(add_rows, [], 0) returns 0. The second is a result whose type differs from the items. One case is a list of table names, which are strings, folded into a total character count, which is an int:

from functools import reduce
def add_length(total, name):
return total + len(name)
tables = ["orders", "users", "refunds"]
print(reduce(add_length, tables, 0))
>>>Output
18

With 0, the first call is add_length(0, "orders"), and the total stays an int: 6, then 11, then 18. Drop the 0 and the first item, the string "orders", becomes the accumulator. The first call then runs "orders" + len("users"), which is "orders" + 5, and Python raises TypeError: can only concatenate str (not "int") to str. Without an initializer, the accumulator starts out as the first item, with that item's type.

The dict merge below needs its {} for the first situation. Each file reports row counts per table, the items and the result are both dicts, and on some nights the job receives no files at all. The step copies the accumulator with dict(acc) before adding to it, so it stays pure and returns a new value on every call.

from functools import reduce
def merge_counts(acc, counts):
merged = dict(acc)
for table, n in counts.items():
merged[table] = merged.get(table, 0) + n
return merged
files = [
{"orders": 3, "users": 1},
{"orders": 2},
{"users": 4, "refunds": 1},
]
print(reduce(merge_counts, files, {}))
print(reduce(merge_counts, [], {}))
>>>Output
{'orders': 5, 'users': 5, 'refunds': 1}
{}

Delete both {} arguments and the first file's dict becomes the starting accumulator. The first print stays the same, because merge_counts copies whatever accumulator it receives. The second call then raises the TypeError instead of printing {}.

Order matters, and operator helps

Since the fold runs left to right, the grouping is fixed: reduce(f, [a, b, c]) computes f(f(a, b), c). For addition and merging counts, grouping makes no difference to the answer. For subtraction or division it does. The operator module provides Python's operators as named functions, such as operator.add, operator.mul and operator.getitem, so you can pass them to reduce without writing a def for each.

from functools import reduce
import operator
event = {"user": {"address": {"city": "Lisbon"}}}
path = ["user", "address", "city"]
print(reduce(operator.getitem, path, event))
print(reduce(operator.mul, [2, 3, 4], 1))
>>>Output
Lisbon
24

operator.getitem(d, k) returns d[k], so folding a key path over a nested record descends a level per key: event["user"], then ["address"], then ["city"]. The 1 initializer for the product works the way 0 does for a sum: it is the value that leaves the result unchanged.

Multiple Choice

What does this print?

from functools import reduce
import operator
print(reduce(operator.sub, [10, 3, 2]))

When a plain loop reads better

Reduce reads well when the step is a single named operation and the accumulator is a single value. It reads worse when the accumulator carries several pieces of state or the step needs branches. Here is an average computed both ways:
from functools import reduce
readings = [4, 8, 6]
def step(acc, value):
count, total = acc
return (count + 1, total + value)
count, total = reduce(step, readings, (0, 0))
print(total / count)
count, total = 0, 0
for value in readings:
count += 1
total += value
print(total / count)
>>>Output
6.0
6.0

Both print 6.0. The loop names each piece of state on its own line, while the reduce version packs count and total into a tuple that the reader unpacks mentally at every call. For totals, largest values and smallest values, sum(), max() and min() beat both, since each names its intent in a word.

✓Do
  • Use reduce when the step is a single named function, like operator.add or merge_counts
  • Pass an initializer whenever the sequence can be empty
  • Return a new accumulator from the step instead of changing the old one
✗Don't
  • Don't use reduce to total or find the largest or smallest; use sum, max or min
  • Don't pack 3 or more pieces of state into a tuple accumulator; write the loop
  • Don't put branches in a step function the reader must trace call by call
Loading the challenge

Partial Application With partial

Daily Life
Interviews

functools.partial(func, *args, **kwargs) returns a new callable that remembers some of func's arguments. When you call it, it runs func with the remembered arguments plus the ones you pass now. Fixed positional arguments go in front of the caller's positional arguments, and fixed keywords are merged with the caller's keywords.

from functools import partial
def build_path(env, table, date):
return f"{env}/{table}/{date}"
prod_path = partial(build_path, "prod")
print(prod_path("orders", "2024-05-01"))
parse_hex = partial(int, base=16)
print(parse_hex("ff"), parse_hex("1a"))
>>>Output
prod/orders/2024-05-01
255 26

prod_path needs only table and date, because env is already filled with "prod". parse_hex presets base by keyword, and that works on built-ins like int as well as on functions you write.

Which parameter a preset fills

A fixed positional argument always lands in the leftmost parameter. That works when the value you want to preset comes first in the signature, and silently goes wrong when it comes later. To preset a later parameter, fix it by name.
from functools import partial
def above(value, threshold):
return value > threshold
wrong = partial(above, 1000)
right = partial(above, threshold=1000)
sizes = [250, 4000, 1200]
print(list(filter(wrong, sizes)))
print(list(filter(right, sizes)))
>>>Output
[250]
[4000, 1200]

wrong(x) runs above(1000, x), which asks whether 1000 is larger than x, so the filter keeps the small file and drops the 2 large ones. No error is raised, and the result is reversed. right fills threshold and leaves value open for filter to supply.

Overriding and colliding arguments

A keyword fixed by partial behaves like a default: the caller can pass the same keyword again and the caller's value wins. A partial object also exposes its parts as .func, .args and .keywords, which helps when a log line should say what a preset function will do.

from functools import partial
def scale(value, factor, offset=0):
return value * factor + offset
to_ms = partial(scale, factor=1000)
print(to_ms(2.5))
print(to_ms(3, offset=7))
print(to_ms(3, factor=60))
print(to_ms.func.__name__, to_ms.args, to_ms.keywords)
>>>Output
2500.0
3007
180
scale () {'factor': 1000}

The third call replaces factor with 60, giving 3 * 60 + 0, and the stored .keywords stay untouched. Positional values cannot be overridden this way. If the caller's positional arguments reach a parameter that is already fixed by keyword, Python raises TypeError and names the parameter that received 2 values.

Multiple Choice

What happens when this runs?

from functools import partial
def tag(prefix, name, suffix):
return prefix + name + suffix
raw = partial(tag, "dw_", suffix="_raw")
print(raw("orders"))
print(raw("orders", "_v2"))

Partial or a small def?

✓partial(build_path, "prod")
  • A single line, with no new def to read
  • Links back to the original through .func
  • Presets arguments and adds no logic
✗def prod_path(table, date)
  • Can add checks, formatting or logging around the call
  • Has its own name and docstring
  • Takes 2 or more lines for what may be a pure preset

Use partial when you hand a function to map, filter or sorted(key=...) and it needs an extra argument those tools never pass. map calls its function with 1 argument per item, so presetting ndigits turns round into a function map can use:

from functools import partial
prices = [3.14159, 2.71828, 9.5]
print(list(map(partial(round, ndigits=2), prices)))
>>>Output
[3.14, 2.72, 9.5]
TIP
Stacking presets costs nothing extra: partial(partial(build_path, "prod"), "orders") produces a single partial object whose .args is ("prod", "orders"), so a call reaches build_path in one step.
Loading the challenge

Nested Functions

Daily Life
Interviews

A def can sit inside another function's body. Python executes that inner def each time the outer function runs, building a fresh function object and binding it to a local name. Like every local, the name goes away when the outer function returns, so no other code in the module can call the helper.

def clean_emails(raw_emails, domain):
def normalize(email):
return email.lower().replace(" ", "")
def belongs(email):
return email.endswith("@" + domain)
cleaned = map(normalize, raw_emails)
return list(filter(belongs, cleaned))
raw = [" Ana@Shop.io", "bo@other.com", "CY@shop.io "]
print(clean_emails(raw, "shop.io"))
>>>Output
['ana@shop.io', 'cy@shop.io']

normalize and belongs exist only while clean_emails runs, so anyone reading the module sees a single public function. belongs also uses domain without receiving it as a parameter: it reads the variable from the function it sits inside. That keeps belongs at 1 parameter, which is what filter passes per item.

Written at module level, belongs would need domain as a second parameter, and you would bridge the gap with partial(belongs, domain=domain). The nested version reaches the same result by reading domain from the scope around it.

How Python finds a name

When a nested function uses a name, Python searches 4 scopes in order and stops at the first match: the inner function's own local names, then the enclosing function's names, then the module's global names, then Python's built-in names such as len and print. Local and global are familiar from earlier work. Enclosing is the layer nesting adds.

ScopeWhere the name is assignedExample from the email code
LocalIn the inner function itself`email`, inside `belongs`
EnclosingIn the function around it`domain`, read by `belongs`
GlobalAt the top level of the module`raw`
Built-inSupplied by Python`map`, `filter`, `list`

Assigning creates a new local

Reading an enclosing name needs no declaration. Assigning to it behaves differently: an assignment anywhere in the inner function makes that name belong to the inner function for its whole body, and the enclosing variable keeps its value.
def summarize(rows):
count = len(rows)
def label():
count = 99
return f"{count} rows"
text = label()
return text, count
print(summarize([4, 8, 15]))
>>>Output
('99 rows', 3)

label gets its own count, set to 99, while the count in summarize stays 3. Rebinding the enclosing variable takes a keyword of its own, which arrives with closures.

Multiple Choice

What happens when this runs?

def load(rows):
def is_valid(row):
return row["id"] is not None
return list(filter(is_valid, rows))
print(len(load([{"id": 1}, {"id": None}])))
print(is_valid({"id": 2}))

When to nest and when not to

✓Do
  • Nest a helper that only 1 function uses and that reads that function's parameters
  • Nest to hand map, filter or sorted a 1-argument function built from outer values
  • Keep nested helpers to a few lines so the outer function stays readable
✗Don't
  • Don't nest a helper you want to test alone; a test can't reach a local function
  • Don't nest logic that 2 functions need; move it to module level
  • Don't stack 3 levels of nesting; every level adds a scope to track

Python builds a fresh function object for each nested def every time the outer function runs. The cost is small, but for a function called millions of times inside a loop, a module-level helper skips it.

Loading the challenge

Closures

Daily Life
Interviews

A nested function can also be the return value. When make_range_check below returns check, its parameters low and high would normally disappear with the call, yet check still reads them, so Python keeps them alive and attaches them to the returned function. A function packaged with the enclosing variables it uses is a closure. A function that builds and returns closures is a function factory.

from typing import Callable
def make_range_check(low: int, high: int) -> Callable[[int], bool]:
def check(value: int) -> bool:
return low <= value <= high
return check
valid_age = make_range_check(0, 120)
valid_pct = make_range_check(0, 100)
print(valid_age(110), valid_pct(110))
print(list(filter(valid_pct, [-5, 40, 100, 130])))
>>>Output
True False
[40, 100]

valid_age and valid_pct run identical code and answer differently, because each call to make_range_check creates a new low and high, and the returned check carries that call's pair. The hint Callable[[int], bool] says the factory returns a function that takes an int and returns a bool.

Factories that turn up in pipelines
  • ▸make_range_check(low, high): a validator per column, built from a config
  • ▸make_prefixer("stg_"): a renamer per pipeline layer
  • ▸make_counter(name): a running count per table, kept between calls
Loading the challenge

Where captured variables live

Python stores each captured variable in a cell, a small container that the outer function and the closure share. A function lists the names it captures in __code__.co_freevars and holds the cells themselves in __closure__, a tuple you can index.

def make_prefixer(prefix):
def add(name):
return prefix + name
return add
staging = make_prefixer("stg_")
print(staging("orders"))
print(staging.__code__.co_freevars)
print(staging.__closure__[0].cell_contents)
>>>Output
stg_orders
('prefix',)
stg_

A cell is the variable's storage, so the closure reads whatever the cell holds at the moment it runs. If the outer function rebinds prefix before returning, the closure sees the new value, and the last section of this lesson shows that behaviour turning into a bug.

Keeping state with nonlocal

In the make_counter factory below, the inner function increment runs count += 1 on the factory's own count, which starts at 0. Assigning to a captured name follows the rule from nested functions: the name becomes local to increment. Without a declaration, count += 1 would try to read a local that has no value yet, and Python raises UnboundLocalError. Declaring nonlocal count at the top of increment tells Python that count belongs to the enclosing function, so the assignment updates the shared cell instead.

Every call to make_counter creates a fresh cell, so 2 counters keep separate counts:

def make_counter(name):
count = 0
def increment():
nonlocal count
count += 1
print(f"{name}: {count}")
return count
return increment
orders = make_counter("orders")
users = make_counter("users")
orders()
orders()
users()
orders()
orders=
users=
output0 lines
defines make_counter
1/32

Each make_counter call creates its own count cell, so the third orders() call continues from 2.

TIP
Mutating a captured list or dict, as in seen.append(row_id), needs no nonlocal, because it changes the object and leaves the name bound to it. You need nonlocal when the inner function assigns to the name itself, with = or an augmented operator like +=.
✓Closure factory
  • Wraps new logic around the captured values
  • Can update its state between calls with nonlocal
  • Lists captured names in __code__.co_freevars
✗partial
  • Presets arguments of a function that already exists
  • Keeps its preset values fixed for its whole life
  • Lists preset values in .args and .keywords
Multiple Choice

What does the last line print?

def make_tracker():
total = 0
def add(n):
nonlocal total
total += n
return total
return add
a = make_tracker()
b = make_tracker()
a(5)
b(2)
print(a(10), b(1))

Leave out the nonlocal line and the factory itself runs without complaint: the failure waits for the first call to the returned function. Python settles each name's scope when it reads the inner def, before any line of it runs, so count += 1 makes count local to the whole body. The traceback's last line reads UnboundLocalError: local variable 'count' referenced before assignment, and the frame above it names the inner function and its count += 1 line. An assignment in an inner function to a name the outer function defines is the signal to add nonlocal.

Loading the challenge

The Late-Binding Closure Trap

Daily Life
Interviews
A closure reads a captured variable at the moment it runs, from the cell it shares with the enclosing function. Create closures inside a loop and they all capture the same loop variable, because the loop rebinds a single name on every pass. Call them after the loop and every one of them sees the loop's final value.

Here is the pricing bug from the start of the lesson. Each price function should apply its own city's surge multiplier, and the last 2 prints check what the functions share.

def build_pricers(multipliers):
pricers = {}
for city, rate in multipliers.items():
def price(fare):
return fare * rate
pricers[city] = price
return pricers
surge = {"Chicago": 1.2, "Boston": 1.5, "Austin": 1.8}
pricers = build_pricers(surge)
for city, fn in pricers.items():
print(city, fn(10))
chicago, austin = pricers["Chicago"], pricers["Austin"]
print(chicago is austin)
print(chicago.__closure__[0] is austin.__closure__[0])
fn=
city=
austin=
chicago=
surgemultipliers
:
:
:
pricers
:
:
:
output0 lines
defines build_pricers
1/33

All 3 functions read the same rate cell, which holds 1.8 once the loop ends.

The False confirms there are 3 distinct function objects in the dict. The True shows what they have in common: the same cell for rate. There is a single rate variable in build_pricers, the loop rebinds it 3 times, and none of the functions runs until the loop is over and rate holds 1.8.

When the trap fires and when it doesn't

The bug needs 2 ingredients: a closure created inside a loop, and a call that happens after the loop variable has moved on. Call price(10) inside the loop body, right after the def, and you get 12.0, 15.0 and 18.0, because each call reads rate while it still holds the current city's multiplier. The trap shows up in code that stores functions for later: dicts of pricers, lists of validation rules, handlers registered to run at the end of a job.

Signs of the trap in a code review
  • A def inside a for loop whose body reads the loop variable
  • Functions stored in a list or dict and called after the loop ends
  • Every stored function returning the result meant for the last item

Three ways to bind each value now

Each fix moves the binding from call time to creation time, so every function holds its own value.
//

Fix 1: a default argument

Default values are evaluated once, when the def statement runs. Writing def price(fare, rate=rate) copies the current rate into that function's default on each pass, so the function stops reading the shared cell.

//

Fix 2: a factory function

Move the def into a factory such as make_pricer(rate). Every factory call runs in its own scope with its own rate cell, and nothing rebinds that cell after the call returns.

//

Fix 3: partial

partial(multiply, rate) evaluates rate immediately and stores the value in the partial object's .args. All 3 fixes, side by side in a single run:

from functools import partial
surge = {"Chicago": 1.2, "Boston": 1.5, "Austin": 1.8}
def make_pricer(rate):
def price(fare):
return fare * rate
return price
def multiply(rate, fare):
return fare * rate
with_default, with_factory, with_partial = {}, {}, {}
for city, rate in surge.items():
def price(fare, rate=rate):
return fare * rate
with_default[city] = price
with_factory[city] = make_pricer(rate)
with_partial[city] = partial(multiply, rate)
for city in surge:
print(city, with_default[city](10), with_factory[city](10), with_partial[city](10))
>>>Output
Chicago 12.0 12.0 12.0
Boston 15.0 15.0 15.0
Austin 18.0 18.0 18.0
FixWhen the value is boundTrade-off
Default argument `rate=rate`When the `def` runs, once per passA caller can pass `rate=` and override it
Factory `make_pricer(rate)`When the factory is called, in a new scopeAn extra function to name and read
`partial(multiply, rate)`When the partial object is createdNeeds a function whose first parameter is the rate, or a keyword

The default-argument fix leaves rate as a real parameter, so a caller who passes a second argument replaces the stored multiplier and gets no error. The stored value sits in the function's __defaults__ tuple, the counterpart of __closure__ for this fix:

surge = {"Chicago": 1.2, "Boston": 1.5}
pricers = {}
for city, rate in surge.items():
def price(fare, rate=rate):
return fare * rate
pricers[city] = price
print(pricers["Chicago"](10))
print(pricers["Chicago"](10, 3.0))
print(pricers["Chicago"].__defaults__)
>>>Output
12.0
30.0
(1.2,)

The second call returns 30.0 because 3.0 arrives by position and fills rate. The (1.2,) in __defaults__ is untouched, so the next 1-argument call returns 12.0 again. A default works for any loop variable that a stored function reads, and a column name is fixed the same way as a multiplier.

Loading the challenge
Multiple Choice

What does this print?

def build_rules():
rules = []
for limit in [10, 20, 30]:
def check(n):
return n < limit
rules.append(check)
limit = 5
return rules
for rule in build_rules():
print(rule(8))
PUTTING IT ALL TOGETHER

> A nightly data quality job checks 40 tables before the morning dashboards refresh. A config dict maps each column to its allowed range, and the job must build a validator per column, run every validator over each batch of rows, and combine the per-batch failure counts into a single report.

A factory make_range_check(low, high) builds each column's validator as a closure, so the rule's logic is written once and the config supplies the numbers
The validators are created in a loop over the config, so calling the factory inside the loop, rather than writing a bare inner def, gives every column its own range
Where an existing function needs the column name preset, partial(count_failures, column=name) hands map a function that takes 1 row at a time
Small nested helpers such as is_null(row) live inside the batch function and read its settings without adding names to the module
Per-batch failure dicts are merged with reduce(merge_counts, batches, {}), and the {} initializer keeps a night with no batches from raising TypeError
KEY TAKEAWAYS
reduce(step, items, start) folds left to right, and passing start keeps an empty sequence from raising TypeError
Prefer sum, max, min or a loop when the step needs branches or several pieces of state
partial fills the leftmost positional parameters; preset later ones by keyword, and callers can override preset keywords
A nested def is a local name: hidden from the rest of the module, rebuilt per call, able to read enclosing variables
A returned inner function is a closure; its captured variables live in cells, and nonlocal lets it rebind them
Closures read their cells at call time, so closures built in a loop share the final value unless a default, a factory or partial binds each one

Fold sequences with reduce, preset arguments with partial, and build closures that capture the values you meant.

Category
Python
Difficulty
intermediate
Duration
31 minutes
Challenges
19 hands-on challenges

Topics covered: Folding a Sequence With reduce, Partial Application With partial, Nested Functions, Closures, The Late-Binding Closure Trap

Lesson Sections

  1. Folding a Sequence With reduce (concepts: Flexible Arguments)

    Watch it fold 4 batch row counts. The step function prints every call, so you can follow the accumulator from call to call. Giving reduce a starting value Order matters, and operator helps When a plain loop reads better Reduce reads well when the step is a single named operation and the accumulator is a single value. It reads worse when the accumulator carries several pieces of state or the step needs branches. Here is an average computed both ways:

  2. Partial Application With partial (concepts: Unpacking)

    Which parameter a preset fills A fixed positional argument always lands in the leftmost parameter. That works when the value you want to preset comes first in the signature, and silently goes wrong when it comes later. To preset a later parameter, fix it by name. Overriding and colliding arguments Partial or a small def?

  3. Nested Functions (concepts: Scope)

    How Python finds a name Assigning creates a new local Reading an enclosing name needs no declaration. Assigning to it behaves differently: an assignment anywhere in the inner function makes that name belong to the inner function for its whole body, and the enclosing variable keeps its value. When to nest and when not to Python builds a fresh function object for each nested def every time the outer function runs. The cost is small, but for a function called millions of times inside a loop, a modu

  4. Closures (concepts: Scope)

    Where captured variables live Keeping state with nonlocal

  5. The Late-Binding Closure Trap (concepts: Functions)

    A closure reads a captured variable at the moment it runs, from the cell it shares with the enclosing function. Create closures inside a loop and they all capture the same loop variable, because the loop rebinds a single name on every pass. Call them after the loop and every one of them sees the loop's final value. When the trap fires and when it doesn't Three ways to bind each value now Each fix moves the binding from call time to creation time, so every function holds its own value. Fix 1: a d