0Pricing
Python Academy · Lesson

Partial Functions

Use functools.partial.

Partial Functions is a free Python Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is a partial?

functools.partial creates a new function with some arguments of an existing function already filled in. It is like pre-loading arguments so you can call the result with fewer.

Import it with from functools import partial.

from functools import partial
def power(base, exp):
    return base ** exp
square = partial(power, exp=2)
print(square(5))

Fixing positional arguments

Positional arguments passed to partial fill the function's parameters from the left. Remaining arguments are supplied when you call the partial.

from functools import partial
def multiply(a, b):
    return a * b
double = partial(multiply, 2)
print(double(10))
print(double(50))

Fixing keyword arguments

Keyword arguments given to partial become defaults that you can still override at call time.

from functools import partial
def greet(name, greeting='Hello'):
    return greeting + ', ' + name
hola = partial(greet, greeting='Hola')
print(hola('Ada'))
print(hola('Bob', greeting='Hey'))

Partials with built-ins

You can partial-ize built-in functions too. Here we make a base-2 integer parser from int.

from functools import partial
binary = partial(int, base=2)
print(binary('1010'))
print(binary('1111'))

Cleaner callbacks

Partials are great for callbacks where you must pass a no-argument or single-argument function but want to preload context.

from functools import partial
def log(level, message):
    return '[' + level + '] ' + message
error = partial(log, 'ERROR')
print(error('disk full'))

Partials in map

A partial slots neatly into higher-order functions like map, fixing one argument while the other varies per item.

from functools import partial
def add(a, b):
    return a + b
add10 = partial(add, 10)
print(list(map(add10, [1, 2, 3])))

Inspecting a partial

A partial object exposes the wrapped function via .func, the fixed positional args via .args, and fixed keywords via .keywords.

from functools import partial
def f(a, b, c):
    return (a, b, c)
p = partial(f, 1, c=3)
print(p.func.__name__)
print(p.args)
print(p.keywords)

Stacking partials

You can build a partial from another partial, layering more fixed arguments step by step.

from functools import partial
def volume(l, w, h):
    return l * w * h
base = partial(volume, 2)
slab = partial(base, 3)
print(slab(4))

Partial vs lambda

A lambda can do the same job, but partial is often clearer for fixing arguments and exposes the wrapped function for introspection.

from functools import partial
def power(base, exp):
    return base ** exp
cube_p = partial(power, exp=3)
cube_l = lambda b: power(b, 3)
print(cube_p(2), cube_l(2))

A real example: rounding

Make a two-decimal rounder by partial-izing the built-in round.

from functools import partial
round2 = partial(round, ndigits=2)
print(round2(3.14159))
print(round2(2.71828))

partialmethod for classes

The related functools.partialmethod does the same for methods inside a class definition, pre-filling arguments on a method.

from functools import partialmethod
class Light:
    def set(self, state):
        self.state = state
        return state
    on = partialmethod(set, True)
    off = partialmethod(set, False)
l = Light()
print(l.on())
print(l.off())

Quick Check

What does functools.partial do?

Recap: Partial Functions

You learned that functools.partial:

  • Creates a new function with some arguments pre-filled (positional or keyword).
  • Lets you override preset keyword defaults at call time.
  • Is ideal for callbacks, map, and reducing repetition.
  • Exposes .func, .args, .keywords, and has a method variant partialmethod.
from functools import partial
add = lambda a, b: a + b
plus1 = partial(add, 1)
print(plus1(41))

Frequently asked questions

Is the “Partial Functions” lesson free?

Yes — the full text of “Partial Functions” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Partial Functions”?

Use functools.partial. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Partial Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. First-Class Functions
  2. Closures and Free Variables
  3. nonlocal and Mutable State
  4. Partial Functions
← Back to Python Academy