# Partial Function Application In Python

Sep 10th, 2021 - written by Kimserey with .

Partial function application is a useful tool to reduce the signature of a function by fixing the first arguments which then result in a new function with the remaining arguments. In this post, we will see when partial application is useful in Python and how we can implement it.

## partial

Partial application is the process of fixing a number of arguments of a function. This then creates a new function taking the remaining arguments as parameter.

Using this method, we can create reduce the complexity of functions with multiple arguments by creating sub-functions which have a tighter scope by fixing the first arguments.

Partial application can be achieve using partial from functools:

1
>>> from functools import partial


For example let’s consider an addition function:

1
2
3
4
5
6
7
>>> def add(x, y):
return x + y

>>> add_to_two = partial(add, 2)

>>> add_to_two(3)
>>> 5


Here we have a function add taking two arguments x and y. We partially apply it by setting x as 2 which result in a function taking a single argument.

Because we’ve set the first argument, we have reduced the complexity of the function by making it’s scope smaller - here the function nows adds 2 to any number provided.

## partialmethod

Partial application can also be achieved on methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> from functools import partialmethod

>>> class A:
def __init__(self):
self._value = 0

@property
def value(self):
return self._value

def set_value(self, value):
self._value = value

set_two = partialmethod(set_value, 2)


In this example, we have created a class A with a value property, and a set_value method. By using partialmethod, we are able to set the argument of set_value and create set_two.

set_two no longer takes any arguments:

1
2
3
4
5
6
7
8
9
>>> a = A()

>>> a.value
>>> 0

>>> a.set_two()

>>> a.value
>>> 2


We can see that calling set_two has set value to 2.

## reduce

reduce is a good example where partial application comes handy. reduce is also available from functools, taking a function as first argument and an iterable as second argument, it folds the iterable over by continuously calling the first argument feeding back to it it’s own result as first argument.

1
2
3
4
>>> from functools import reduce

>>> reduce(lambda acc, cur: acc + cur, [1, 2, 3, 4])
>>> 10


For example here, we use an addition to compute the sum of the list [1, 2, 3, 4]. Using reduce and combining it with partial, we can then fix the first argument (the lambda) and create a simplified version:

1
2
3
4
5
6
7
>>> partial
>>> functools.partial

>>> new_sum = partial(reduce, lambda acc, cur: acc + cur)

>>> new_sum([1, 2, 3, 4])
>>> 10


Here we’ve created a sum function new_sum by partially applying our addition as first argument of reduce. And that concludes today’s post!

## Conclusion

In this post, we looked into how we could use partial and partialmethod to partially apply argument to functions. And we finished with a concrete example using reduce where we partially applied the first argument to create a new function. I hope you liked this post and I see you on the next one!

Designed, built and maintained by Kimserey Lam.