Bind and forward function arguments
Why you care
Every wrapper function in an ETL codebase — retry adapters, logging decorators, generic loaders — must accept arguments it does not understand and forward them without modification. If you cannot distinguish positional-only from keyword-only parameters, adding one to a shared utility will silently break every caller that passes them in the wrong form, and the error only surfaces at runtime with a TypeError that names the parameter but not which caller is wrong. Interviewers ask about *args and **kwargs because the answer separates engineers who copy patterns from engineers who can reason about what a function signature actually enforces.
Explain like I'm 5
Think of a restaurant order form. The first three boxes are numbered: they expect item, quantity, table — in that order and in those positions; you cannot write "quantity" in box one. A second section has labelled fields: notes:, spice level:. These can appear in any order because the label tells the waiter which is which. *args is a pocket for any extra numbered items; **kwargs is a pocket for any extra labelled ones. A wrapper who takes your whole form and hands it to the kitchen untouched never needs to know what is on it.
Core concept
Python function parameters fall into five categories, controlled by two separators:
- Positional-only (before
/): callers must pass by position; they cannot saytable="orders". - Regular (between
/and*): callers may pass by position or by name. - Keyword-only (after a bare
*or after*args): callers must name them;dry_run=True, not justTrue. *args: collects all extra positional arguments into a tuple.**kwargs: collects all extra keyword arguments into a dict.
Forwarding rule: func(*args, **kwargs) unpacks a tuple into positional slots and a dict into keyword slots. A thin wrapper that takes (*args, **kwargs) and passes them straight through is compatible with any callee signature.
Use positional-only (/) when the parameter name is an implementation detail you might rename (e.g. open(file, mode, /)). Use keyword-only (*) when passing a value by position would be ambiguous — every boolean flag qualifies. load(path, True) conveys no intent; load(path, dry_run=True) does.
When you cannot know what arguments a callee will need in the future, accept **opts and forward it. When the signature stabilises, replace **opts with explicit keyword-only parameters so callers get IDE autocomplete and early TypeError on typos.
Minimal example Separators and forwarding
def load(table, count, /, *, dry_run=False, batch=500):
return (table, count, dry_run, batch)
print(load("orders", 200))
print(load("orders", 200, dry_run=True, batch=100))
def wrapper(*args, **kwargs):
return load(*args, **kwargs)
print(wrapper("trades", 50, dry_run=False))('orders', 200, False, 500)
('orders', 200, True, 100)
('trades', 50, False, 500)In production Generic ETL pipe with forwarded writer options
The caller controls CSV formatting; etl_pipe does not need to know what options exist.
import csv
from pathlib import Path
def write_rows(path, rows, /, *, delimiter=","):
"""path and rows are positional-only; delimiter is keyword-only."""
Path(path).write_text(
"\n".join(delimiter.join(str(c) for c in r) for r in rows)
)
def etl_pipe(src, dst, transform, /, **writer_opts):
"""Caller controls writer options; etl_pipe does not need to know them."""
with open(src, newline="") as f:
rows = [transform(r) for r in csv.DictReader(f)]
write_rows(dst, rows, **writer_opts)
return len(rows)
Path("orders.csv").write_text("id,amount\n1,250\n2,90\n3,410\n")
n = etl_pipe("orders.csv", "doubled.csv",
lambda r: [r["id"], int(r["amount"]) * 2])
print(f"{n} rows written")
print(Path("doubled.csv").read_text())3 rows written
1,500
2,180
3,820In your world
The situation
Your team has a shared run_loader(table, rows, batch_size=1000) called in 40 Jenkins jobs. A migration adds strict_mode: bool = False to the underlying Postgres writer. The first engineer to need it adds it as a positional parameter. A second engineer, six months later, decides to swap the order of batch_size and strict_mode.
What actually breaks
Every job that passes either value positionally breaks with a TypeError. The test suite mocks the writer, so none of the tests catch it. Eight jobs fail in production before anyone notices. The post-mortem takes longer than the fix.
What you'd do on Monday
Make every configuration flag keyword-only from the start. Then run_loader("orders", rows, batch_size=1000) is explicit and order-immune. New options can be added without a flag day.
def run_loader(table, rows, /, *, strict_mode=False, dry_run=False, batch_size=1000):
# table and rows are positional-only: no accidental name coupling.
# new keyword-only options can be added without touching any caller.
if dry_run:
print(f"[dry] would load {len(list(rows))} rows into {table}")
return 0
return _write_to_postgres(table, rows,
strict_mode=strict_mode,
batch_size=batch_size)Imagine you work at Paytm
The numbers
Illustrative: 500M events ÷ 86,400 s ≈ 5,800 events/s peak, running across ~200 worker processes each calling a shared serialisation helper. If that helper gains a new positional parameter and 40 of 200 workers are deployed before the rest, those 40 crash on every call that passes the parameter positionally.
What the Scenario-A answer does here
A single-process ETL fixes argument errors at startup. At 200 workers, a partial deploy means partial breakage: 40 workers crash with TypeError, 160 continue, and the error rate in the monitoring dashboard climbs for twenty minutes before an on-call engineer investigates.
What flips at this scale
At this scale, argument contracts are versioned. You freeze a positional-only interface for the stable core (table name, payload), accept everything else as **opts, and validate opts keys at startup with an explicit allowlist. New options roll out without a flag day because callers that do not pass the option still work.
The trade-off you'd defend
I would defend strict positional-only for the first two parameters that will never change, plus **opts with an allowlist for everything else, trading some IDE autocomplete clarity for deployment independence.
Illustrative numbers chosen to make the arithmetic visible; not Paytm-published figures.
Gotchas
Passing a keyword argument by position past a /
TypeError: load() got some positional-only arguments passed as keyword arguments.
A parameter declared before / cannot be named by the caller; the name is the function's private concern.
Drop the name; pass by position only. Or remove / if the name is stable and callers knowing it is fine.
Unknown kwargs silently ignored by a wrapper that does not forward
dry_run=True is accepted without error, but the data is written anyway and no warning appears.
The wrapper accepts **kwargs but never passes it to the inner call. The extra keyword disappears.
Always forward: inner(*args, **kwargs). If you only want specific extras, check them explicitly and raise on unrecognised keys.
Passing the same argument positionally and by name
TypeError: load() got multiple values for argument 'table'.
load("orders", table="orders") fills the table slot twice.
Pass either by position or by name, never both.
Tricky questions
*args appear before a positional-only parameter?No; *args always comes last.
*args can appear after /. For example def f(a, b, /, *args, kw=True) is valid and common. What you cannot do is declare a positional-only parameter after *args, because everything after *args is already keyword-only.
**kwargs capture every keyword argument the caller passes?Yes, every keyword argument.
Only keyword arguments that do not match an explicit parameter. In def f(a, **kw), calling f(1, a=2) raises TypeError: got multiple values for argument 'a' — a is not in kw because it is already a named parameter.
Scenario question
You're handed send_alert(team, message, level="ERROR") used in 40 places. You need to add a channel parameter that defaults to "slack", but three callers should be able to set it to "pagerduty". How do you change the signature while touching the fewest callers?
Model answer
I'd add channel="slack" as a keyword-only parameter after a *: def send_alert(team, message, *, level="ERROR", channel="slack"). The 40 existing callers pass team and message positionally and never mention level or channel, so they are unchanged. The three new callers add channel="pagerduty" explicitly. The trade-off is that any caller currently passing level positionally — send_alert("data", "failed", "WARN") — will break, so I'd grep for that pattern first.
Interview corner
CommonWhat is the difference between *args and **kwargs?
*args collects extra positional arguments into a tuple and **kwargs collects extra keyword arguments into a dict. Together they let a function accept anything and forward it — useful in wrappers and decorators where the outer function does not need to know the inner function's signature.”TrickCan you call a positional-only parameter by name?
/ are positional-only: Python raises a TypeError if you try to name them. That lets library authors rename internal parameters in a future version without breaking callers.”Experience checkWhere have you used **kwargs forwarding in real ETL work?
(*args, **kwargs) and passes them through unchanged, so it does not need updating every time the underlying loader gains a new option.”Memory hook
Numbered order-form boxes cannot be labelled; labelled fields cannot be positional — the / is the line between them, and * starts the labelled section.
Use keyword-only flags for every boolean parameter so the call site reads as intent.
Self-check
What does the
/separator mean in a function signature?Every parameter before
/is positional-only: callers cannot name it. The function can rename it in a future version without breaking callers.What happens if you call
f(1, a=1)whenf(a, **kw)is the signature?TypeError: f() got multiple values for argument 'a'—1fillsapositionally anda=1tries to fill it again by name.How do you forward all arguments a wrapper receives to an inner function?
return inner(*args, **kwargs)inside a wrapper defined asdef wrapper(*args, **kwargs).
Write def strict_load(table, rows, /, *, batch_size=500, dry_run=False). Call it four ways: (a) correctly with strict_load("orders", [], batch_size=100); (b) with table= as keyword — catch the TypeError and print it; (c) with dry_run passed as the third positional — catch the TypeError and print it; (d) through a def relay(*a, **kw): return strict_load(*a, **kw) using the same args as (a). Paste the four results.