# Partial functions#

In some use-cases it might be necessary to collect a function together with some parameters together in one variable to execute it later. functools’ partial() enables to do this.

from functools import partial


For demonstrating it, we define an example function.

def compute_sum(a, b):
return a + b


We can then store the partial object of that function in combination with the parameters in a variable.

sum_of_3_and_4 = partial(compute_sum, 3, 4)


This object is callable.

sum_of_3_and_4()

7


## Keeping parameters unset#

It is also possible to only partially specify parameters.

sum_of_3_and_b = partial(compute_sum, 3)

sum_of_3_and_b(9)

12


## Passing keyword arguments#

It also works with keyword arguments.

sum_of_a_and_4 = partial(compute_sum, b=4)

sum_of_a_and_4(5)

9

sum_of_a_and_4(a=1)

5


## Exercise#

Program a function that determines the Euclidean distance of two points. Afterwards, use partial to pass one fixed point and a for-loop to print out distances of a list of points.

fixed_point = (1, 1, 1)
list_of_points = [(2,1,1), (1,1,3), (2,2,2)]