Configuring Functions and Methods with functools.partial
Learn how functools.partial configures callable arguments and how functools.partialmethod creates method variants while preserving instance binding.
Intermediate
Sometimes a function already does the right work, but callers repeatedly need to supply the same configuration.
functools.partial() creates a new callable with some arguments supplied in advance. It does not run the original function when the partial object is created; it calls the original function when the new callable is invoked.
Build a configured callable
Use
partial(function, *args, **keywords) to store leading positional arguments and keyword arguments for later calls. This can make a general function easier to reuse without defining another wrapper function just to pass the same configuration every time.
Output
Ready to run.
The first call supplies the amount and reuses the bound currency. The second call supplies a different precision while retaining the same currency. The partial object stores the configuration; it does not store the result of either call.
Understand positional argument order
Positional arguments stored in
partial() are placed at the beginning of the original call. Positional arguments passed later are appended after those stored arguments. This makes binding leading arguments straightforward, but it does not create an empty slot for a later positional parameter.
Output
Ready to run.
The stored opening delimiter fills the first parameter,
left. Later positional arguments fill the next unbound positional parameters in order. Binding a later parameter by keyword can be useful when you do not want to supply every earlier parameter positionally.
Quick check
What value is printed when the pre-bound first argument is followed by the two call-time arguments?
from functools import partial
def join(left, middle, right):
return left + middle + right
wrap = partial(join, "<")
print(wrap())
Override a pre-filled keyword when needed
A keyword passed when creating a partial object acts as the default for that keyword in the eventual call. A caller may pass the same keyword again, and the later value overrides the pre-filled one. This is useful when one configuration is typical but exceptions should remain possible.
Output
Ready to run.
Quick check
Which value replaces the stored prefix for this call?
from functools import partial
def label(message, *, prefix):
return f"{prefix}{message}"
draft = partial(label, prefix="Draft: ")
print(draft("Report", prefix=))
Adapt a function for a callback
APIs that accept a callback often call it with only the input value.
partial() can configure an existing function so it fits that calling shape. This avoids introducing a one-line
lambda or named wrapper when the only goal is to pre-fill arguments.
Output
Ready to run.
Here
map() passes each measurement as the first argument to
round(), while the partial object supplies
ndigits=1. The configured callable has the one-argument shape that
map() expects.
Quick check
Which configured callable rounds each item to one decimal place?
from functools import partial
values = [1.24, 1.26]
round_values = partial(round, ndigits=)
print(list(map(round_values, values)))
Inspect a partial object and choose the right tool
Partial objects expose
.func,
.args, and
.keywords so you can inspect the original callable and the stored configuration. They do not automatically copy the original function’s
__name__ or
__doc__ attributes. Use this when debugging or documenting a configured callable rather than assuming it is a normal function object.
Output
Ready to run.
Choose
partial() when you want to configure arguments for later calls. Choose
cache() or
lru_cache() when you want to reuse results from earlier calls; partial application and result caching solve different problems. A partial object does not memoize calls.
Configure method variants with functools.partialmethod
Use
functools.partialmethod() when you want several methods to share one implementation but use different pre-filled arguments. It is designed for class definitions: when the method is accessed through an instance, Python binds that instance as
self before applying the arguments configured by
partialmethod(). This method binding is the important difference from creating an ordinary partial object for a standalone call.
Output
Ready to run.
Both names reuse
play() and only fix its
mode argument. The instance is still supplied automatically, and the track remains different for each call. A small number of named variants can make a class interface easier to read without writing near-identical forwarding methods.
Quick check
Which call produces the result "desk: turbo" with the class below?
from functools import partialmethod
class Fan:
def __init__(self, name):
self.name = name
def set_mode(self, mode):
return f"{self.name}: {mode}"
normal = partialmethod(set_mode, "normal")
turbo = partialmethod(set_mode, "turbo")
fan = Fan("desk")
print(fan.)
Quick check
Before using
partial(), identify which arguments should stay fixed, which may vary at each call, and whether the receiving API calls your configured function with the arguments you expect. Ordinary positional binding fills arguments from the left; Python 3.14 added
functools.Placeholder for reserving positional slots; that newer feature is intentionally outside this focused lesson.
After studying these examples, use the quiz to check your understanding and the separate coding tasks to practice configuring callables.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.