Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk3 min

How to Pass a Function as a Parameter in Python

Pass a function by name, invoke it through a parameter, and see how methods, callable objects, annotations, and partial arguments fit the same pattern.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Pass a function by its name, without parentheses, then call the parameter inside the receiving function. For example, apply(double, 3) passes the function object; apply(double(3), 3) calls double first and passes its result instead.

Pass the function, then call it inside the receiver

A function is a value in Python. Giving its name as an argument passes the function itself, which the receiving function can invoke later.

As an Amazon Associate I earn from qualifying purchases.

from collections.abc import Callable

def double(value: int) -> int:
    return value * 2

def apply(fn: Callable[[int], int], value: int) -> int:
    return fn(value)

result = apply(double, 3)  # 6

In apply(double, 3), double has no parentheses, so it is not called at the point of passing. The receiver invokes it with fn(value). By contrast, apply(double(3), 3) evaluates double(3) immediately and passes the resulting integer, not a callable; that does not satisfy this receiver’s contract.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Define what the callback is expected to do

Give the parameter a descriptive name such as transform, predicate, or on_complete. State which arguments the receiver will supply and what it expects in return, or whether it expects a side effect. The callback must accept the arguments used at its call site; a mismatch appears when Python invokes it.

def run_twice(fn, value):
    return fn(fn(value))

run_twice(double, 3)  # Passes double; invokes it twice

Here the receiver calls fn first with value, then with that call’s result. The callback therefore needs to accept the value type it receives on both calls.

Functions, methods, and callable objects all work

Pass a bound method

An instance method accessed through an object, such as worker.process, is a bound method and can be passed just like a function. When called, it already supplies worker as the method’s first argument. Passing Worker.process through the class instead gives the underlying function reference; callers normally supply the instance explicitly.

Pass a callable instance

An instance whose class defines __call__ supports call syntax and can be used as a callback. This is useful when the callback needs to keep state in an object rather than in a function’s surrounding scope.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Annotate the callback when useful

For a fixed list of positional argument types and a return type, use collections.abc.Callable in new annotations. Python’s typing documentation describes Callable[[int], str] as a callable that takes one integer and returns a string; it documents typing.Callable as deprecated. See the Python 3.14 typing documentation.

from collections.abc import Callable

def transform_all(
    values: list[int],
    transform: Callable[[int], str],
) -> list[str]:
    return [transform(value) for value in values]

Callable[..., R] describes a callable with an arbitrary parameter list and return type R, but ordinary Callable cannot fully express keyword-only parameters, overloads, or some variadic signatures. For a callback whose keyword names or parameter kinds are part of its contract, describe its __call__ method with a protocol:

from typing import Protocol

class Combiner(Protocol):
    def __call__(
        self, *values: bytes, maxlen: int | None = None
    ) -> list[bytes]: ...

ParamSpec and Concatenate can help type higher-order functions that forward or add parameters. The typing documentation notes support for these in Callable from Python 3.10; type-checker support and syntax details depend on the Python and typing versions in use.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Adapt a callback or bind arguments in advance

If a callback has the wrong shape for the receiver, adapt it with a small lambda or a named wrapper. To pre-fill some arguments, use functools.partial:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from functools import partial

def multiply(value: int, factor: int) -> int:
    return value * factor

double = partial(multiply, factor=2)

To inspect a callable's signature or check proposed arguments, use inspect.signature(callable) and Signature.bind. The Python 3.14 inspect documentation notes that signatures are available for many callable types, including functools.partial, though some built-ins do not expose enough metadata to inspect.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.