Configure Callables with partial
Core idea
functools.partial(fn, *args, **kwargs) creates a callable that stores arguments for later use. In Python 3.13 the stored positional arguments precede later positional arguments; later keyword arguments can override stored keywords. The original function is not executed during partial creation.
from functools import partialdef convert(rate, units): return rate * unitsconvert_box = partial(convert, 12)print(convert_box(3), convert_box(5))The first positional argument is fixed at 12 for these calls.
Configure a keyword
from functools import partialdef label(name, prefix="ID"): return prefix + ":" + nametag = partial(label, prefix="BOX")print(tag("A"))print(tag("A", prefix="BAG"))A later keyword replaces the stored keyword.
Observe call timing
from functools import partialdef announce(text): print(text)ready = partial(announce, "loaded")print("configured")ready()Creating the partial does not call announce.
Common mistake
Partial application does not copy mutable bound objects or guarantee that later arguments are compatible. Passing a positional argument for a parameter already bound by keyword can cause TypeError.
Practice
Create a partial that adds a configured shipping fee. Call it for two prices, then configure the fee by keyword and override it once.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Partial application
Check your understanding of partial application.
What is printed?
from functools import partialdef add(a, b): return a + bf = partial(add, 4)print(f(3))What is printed?
from functools import partialdef label(name, prefix): return prefix + "-" + namef = partial(label, prefix="X")print(f("A", prefix="Y"))What is printed?
from functools import partialf = partial(print, "go")print("ready")f()Does partial copy mutable bound arguments?