Codectionary / Developer documentation / Python

functools.partial()

Create a callable with selected arguments already supplied.

How it works

partial is a standard-library callable adapter. It stores a function and some arguments, then combines them with arguments supplied when the adapter is called.

Stored positional arguments go before later positional arguments. Stored keyword values can be overridden by later keywords. Creating a partial does not execute the wrapped function. These examples use the Python 3.11 API, without newer placeholder features.

Choices and common mistakes

Use a small named wrapper instead when adapting arguments requires validation, branching or a clearer public signature.

The adapter retains references to supplied objects. Mutating a stored list or dictionary can change later behaviour; partial does not freeze the object contents.

Try it yourself

Build a configured formatter using partial. Override one stored keyword in a later call and explain which value wins.

Syntax

from functools import partial
configured = partial(function, fixed_argument, option=value)

Examples

Configure a greeting

Reuse one function with a fixed prefix.

from functools import partial
def greet(prefix, name):
    return f"{prefix}, {name}"

hello = partial(greet, "Hello")
print(hello("John"))
  1. partial stores greet and the positional prefix Hello. Calling hello adds John as the next positional argument.
  2. greet runs only at the final call and returns the formatted string. No duplicate greeting implementation is needed.
  3. Use configured callbacks when a framework expects a callable receiving fewer arguments.
  4. Expected output: Hello, John

Override a stored option

Treat configured keywords as defaults for the adapter.

from functools import partial
def label(name, prefix="Task"):
    return f"{prefix}: {name}"

task = partial(label, prefix="Todo")
print(task("Read"))
print(task("Read", prefix="Done"))
  1. The first call uses the stored keyword Todo. The second supplies the same keyword explicitly, replacing it with Done.
  2. The wrapped function receives the merged keyword arguments. This differs from supplying the same positional argument twice.
  3. A configured reporting function can still let individual callers select a different label.
  4. Expected output: Todo: Read Done: Read

Choose a numeric base

Adapt int for a specific input representation.

from functools import partial
binary_integer = partial(int, base=2)
print(binary_integer("101"))
print(binary_integer("11"))
  1. The adapter supplies base=2 while each call supplies a text value. int interprets those characters as binary digits.
  2. 101 means four plus one; 11 means two plus one. The output is printed in ordinary decimal notation.
  3. Use this for a known-format parser, and still handle ValueError when text contains invalid digits.
  4. Expected output: 5 3

Best practices

  • Use a small named wrapper instead when adapting arguments requires validation, branching or a clearer public signature.
  • The adapter retains references to supplied objects. Mutating a stored list or dictionary can change later behaviour; partial does not freeze the object contents.

Runtime & versions

Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Python language reference

Specifications & further reading