Juhana Jauhiainen

Using functools.partial

I’ve gotten used to a functional programming style in JavaScript and I’m finding I want to use same kind of paradigms in Python.

Builtin methods like map() and filter() already help but the functools module provides additional ways to do functional programming.

Let’s look at what the functools.partial function can do.

partial() can be take a function of many parameters and make it a function of few paramters. This is very useful if you have to call the same function
multiple times with almost the same parameters.

Here’s an example

from functools import partial
def greet(greeting, person):
print(f'{greeting} {person}!')
say_hello = partial(greet, 'Hello')
# prints 'Heloo John!'

You can also use keyword arguments with partial to define which parameters are defined.

greet_john = partial(greet, name='John')
# prints 'Howdy John!'
# prints 'Hi John!'

Pretty neat! There are some other interesting functional programming tools in functools module which I’m going to explore in the future.