← Back to Python

functools.partial

PythonMidpython

The Question

What is functools.partial and when would you use it?

What a Strong Answer Covers

  • pre-fills arguments
  • "returns new callable
  • "doesn't call

Senior-Level Answer

`functools.partial` is a higher-order function that creates a new callable with some arguments of an existing function pre-filled (partially applied). Calling the resulting partial object supplies the remaining arguments.

Basic usage:

```python from functools import partial

def power(base, exponent): return base ** exponent

square = partial(power, exponent=2) cube = partial(power, exponent=3)

square(5) # 25 cube(4) # 64 ```

The `partial` object stores a reference to the original function, the pre-filled positional args, and keyword args. When called, it merges the stored args with the new args and invokes the original function.

**When to use it**:

1. **Callback adaptation**: When a callback signature is fixed (e.g., `button.on_click(callback)` expects `callback(event)`) but you need to pass extra context. `partial(handler, user_id=current_user.id)` creates a bound callback without a lambda.

2. **Creating specialized versions of general functions**: `int_from_hex = partial(int, base=16)` creates a specialized int parser. This is more explicit and picklable than a lambda.

3. **`map`/`filter` integration**: `list(map(partial(operator.mul, 2), [1, 2, 3]))` — more readable than a lambda for simple transformations.

4. **Reducing arity for APIs expecting fewer arguments**: When a library expects a `f(x)` but you have a `g(x, config)` and want to pin `config`.

**Partial vs. lambda**: Both achieve partial application. `partial` is preferred when: the function is already named (no anonymous behavior), the result needs to be picklable (lambdas aren't—`partial` objects are), and introspection matters (`partial` preserves `func`, `args`, `keywords` attributes). Lambdas are better for one-line transformations that benefit from inline readability.

`functools.partialmethod` is the class-method equivalent—used when you want to create a method on a class with some arguments pre-bound, useful for creating specialized versions of base class methods.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Correctly explains partial application, demonstrates basic usage, gives two concrete use cases, mentions the pickleability advantage over lambda.

3/3 — Strong Answer

Explains the internal storage mechanism (func, args, keywords), contrasts partial with lambda with specific guidance on when to prefer each, mentions partialmethod, and gives a realistic callback-binding or map/filter example.

Common Mistakes

  • Confusing partial with currying — partial fills multiple args at once; currying always returns a one-argument function
  • Not knowing that lambdas aren't picklable but partial objects are — matters for multiprocessing
  • Forgetting that positional args in partial must come before the function's positional params — order matters
  • Not mentioning partialmethod for class method specialization

Follow-Up Questions

  • What is the difference between functools.partial and currying? — Partial fills some arguments now, getting a callable that takes the rest at once. Currying transforms a multi-arg function into a chain of single-arg functions. partial(f, a, b)(c) vs. curry(f)(a)(b)(c).
  • Why is functools.partial preferred over lambda in multiprocessing code? — Lambdas are not picklable — multiprocessing serializes tasks via pickle. partial objects are picklable, so they can be sent to worker processes. A lambda used as a worker target will raise a PicklingError.
  • How does partial handle the ordering of positional arguments? — Pre-filled positional args are prepended. partial(f, a, b)(c) calls f(a, b, c). You can only pre-fill from the left for positional args. Use keyword args to fill non-leftmost params.
  • What does functools.partialmethod do and when would you use it? — It's like partial but for methods in a class body. Used to create specialized method variants: class Foo: double = partialmethod(multiply, factor=2). Can't use partial here because self isn't available at class definition time.

Related Questions

  • GIL — What It Is and What It Protects
  • ThreadPoolExecutor & ProcessPoolExecutor
  • Decorators — Under the Hood
  • Generators & yield
  • Context Managers

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout