ML Foundations

functools.partial keywords can be overridden

A keyword stored by functools.partial is a default for later calls, not a locked setting.

Example results for functools.partial keywords can be overridden. original keyword: [cached]
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

A partial stores keywords for a future call

functools.partial returns a callable that remembers the original function and captured arguments. If the wrapper was created with bracket="[]", calling it with only a label uses those brackets. The saved keyword is available to the later function call, so the wrapper provides a convenient default. [1]

The captured keyword is not an access-control rule or a permanent restriction. Python documents that keywords supplied when the partial is called merge with stored keywords. A later value for the same key takes precedence. [1, 2]

References: [1], [2]

Show the override directly

Run examples/python_functools_partial_keyword_override.py. The first call uses the stored square brackets. The second supplies bracket="()" at call time, and the wrapped function receives round brackets. Both calls use the same partial object. [1, 2]

This matters if a wrapper is meant to enforce an option. Do not expose a partial with that keyword and assume it is frozen. Use a wrapper that omits the option from its public signature and passes the fixed value internally.

Executed with Python 3.12.14; exact stdout and checksum are recorded in private evidence.

from functools import partial


def render(label, *, bracket="[]"):
    left, right = bracket
    return f"{left}{label}{right}"


square_bracket = partial(render, bracket="[]")
print(square_bracket("cached"))
print(square_bracket("overridden", bracket="()"))

Recorded output

[cached]
(overridden)

References: [1], [2]

Treat stored values as defaults

Use partial when pre-filling arguments makes a reusable callable clearer, and document which values callers may replace. The mechanism is argument binding, not configuration validation. [1, 2]

The example covers one keyword-only parameter and deterministic string output. It does not measure speed or claim that partial is faster than a nested function. Choose the representation for its call contract and test the values callers may pass.

References: [1], [2]

Sources

  1. Python functools.partial
  2. Python call expressions