A callable is an object that can be invoked. Its signature describes the parameters accepted by a call.
Inspect and bind arguments, distinguish collecting from spreading values and locate call errors before the body runs.
A configured stage might be a function, a bound method, a partial, or an
instance with __call__. A dispatcher can call them uniformly, but it must
distinguish a malformed call from a failure inside the stage. Read parameter
slots, bind arguments without executing work, and preserve that distinction
when validating a complete plan.

Build a stage registry from anything callable
For a stage registry, this is good news: it can hold any callable — a plain
function, a partial with settings pre-filled, or a Threshold(100) instance
carrying its own configuration. The caller just calls it.
Preparing this topic’s interactive investigation…
Read a stage's full parameter list
| Kind | Declared as | Filled by |
|---|---|---|
| positional-only | before / |
position only |
| positional-or-keyword | ordinary names | position or name |
| var-positional | *rest |
leftover positionals, as a tuple |
| keyword-only | after * or *rest |
name only |
| var-keyword | **extra |
leftover keywords, as a dict |
def f(a, b=2, *rest, c, **extra):
print(a, b, rest, c, extra)
f(1, 3, 5, 7, c=9, d=0)Stop low and high being swapped
Everything after *rest, or after a bare *, can only be passed by name —
useful the moment two arguments are easy to swap by accident:
TypeErrorRun it and read the message about positional arguments. Fix the call, not the function — the wall is there on purpose.
Don't remove the *. The wall makes clip(150, high=100, low=0) and
clip(150, low=0, high=100) mean the same thing, and makes the swapped
clip(150, 100, 0) impossible to write at all.
The opposite wall, /, makes a name private — the caller can never use it
by keyword, so it can't clash with a **kwargs key of the same name:
def label(name, /, **tags):
return f"{name} {tags}"
print(label("disk", name="dup"))Watching one call bind, slot by slot
inspect.signature gives you Python's own binding logic as a tool. bind(...)
does exactly what a real call would do to fill the slots, without calling the
function, and raises the same TypeError if the call is bad. Step through it:
The order the binder works in:
Binding validates shape, not the meaning of values
bind answers whether arguments fit parameter slots. It does not check type
annotations, numeric ranges, permissions, or whether the body will raise. Keep
those validations separate so an internal bug is not reported as bad syntax.
parts is absent from the first mapping, then appears as 2 after defaults
are applied. The zero case binds successfully because the keyword exists;
division fails later. callable(obj) makes an even smaller promise: call syntax
is supported, not that this particular call succeeds.
For the plan exercise, first validate every step's name and signature without running a stage. Only execute when all shapes pass. This prevents known bad configuration from starting half a plan, but it does not make execution atomic: a later stage can still fail after earlier stages caused effects. Report that distinction clearly in a dispatcher.
Try one missing keyword-only argument and one well-bound value that crashes inside the body. Their error paths should remain distinguishable. Some built-in or extension callables do not expose a usable signature; a general plugin system must define how it handles that limitation.
One-liner: why every wrapper says *args, **kwargs
In a definition, def wrapper(*args, **kwargs) means "accept anything."
In a call, func(*args, **kwargs) spreads them back out so the original
binds them exactly as if the caller had called it directly — the wrapper binds its own args tuple and kwargs dict, then forwards their
contents for the original function to bind to its parameters. The cost: the wrapper's
own signature says nothing useful, unless @wraps stored the original as
__wrapped__ for inspect.signature to follow:
Tools that read signatures — including a stage dispatcher — see straight through a well-behaved wrapper, and are blind to one that isn't.
Quick practice
import inspect
def resample(series, rate, *extra, method="mean", **options):
return None
sig = inspect.signature(resample)
try:
sig.bind("cpu")
verdict = "ok"
except TypeError:
verdict = "bad call"
print(verdict)Closure, class, or partial? Use partial when you're only pre-filling an
existing function's arguments. Use a closure for a small amount of private
state and one operation. Use a class with __call__ when the configuration
should be visible (over_100.limit), there's more than one operation, or you
want a readable repr in logs. A dispatcher that just calls things doesn't
care which one it gets — that's the whole point of "callable."
🎯 Your turn
Write call_stage(name, args, kwargs). Look the stage up in the provided
STAGES registry. If the name is unknown, return {"error": "unknown stage"}.
Otherwise, use inspect.signature(stage).bind to check the call before
making it — if binding fails, return {"error": "bad call"}; if it succeeds,
call the stage and return {"ok": result}.
call_stage("scale", [4], {"factor": 2.5})→{"ok": 10.0}call_stage("clip", [150, 0, 100], {})→{"error": "bad call"}call_stage("nope", [], {})→{"error": "unknown stage"}
Hint: bind first, call second. Wrapping the call itself in
try/except TypeError would also catch a TypeError raised by a bug inside
the stage, and misreport it as the config's fault.
Press ▶ Run, then ✓ Check. ✅
Practice — level up
0/6 solvedSolve each in its own editor — Run to try, Check to grade. Stuck? Reveal a Hint, or the full Solution + walkthrough.
The provided THINGS dict maps names to objects. Write callable_names(names) returning, in the given order, only the names whose object is callable.
For the provided target(a, b=2, *rest, c=0, **extra), write bind_report(args, kwargs) that binds the call with inspect.signature(...).bind, applies defaults, and returns {"a", "b", "rest", "c", "extra"} — with rest as a list.
Write a class Threshold(limit) whose instances are callable: calling one with a value returns value >= limit and counts how many values were over the limit in self.over. flag_values(limit, values) returns {"flags": [...], "over": n}.
Write a decorator checked(func): its wrapper binds the call against func's signature and returns the string "bad call" instead of raising if binding fails; otherwise it returns func's result. Keep the identity with @wraps. checked_calls(calls) runs each [args, kwargs] pair against the provided resample, and returns {"signature": str(inspect.signature(wrapped)), "results": [...]}.
A plan is a list of {"stage", "args", "kwargs"} dicts. Write run_plan(plan): first check every step (unknown stage or a call that fails to bind), and if any are bad return {"ok": false, "bad_steps": [indexes], "results": []} without running anything. Otherwise run each step and return {"ok": true, "bad_steps": [], "results": [...]}. Use the provided STAGES.
Explain why decorator wrappers are written as def wrapper(*args, **kwargs) and call func(*args, **kwargs). Then explain what that costs anyone inspecting the decorated function, and how @wraps recovers it.