Python Partial Function – functools partial()

Filed Under: Python
Python Partial Function

What is a Python Partial Function?

Sometimes a function accepts multiple parameters. If there is a situation where we are passing the same parameter to it many times, we can create a python partial function. In the partial function, some of the parameters are fixed. It’s useful in creating a more meaningful function name from an existing function.

How to Create Partial Function in Python?

Python functools partial() function is used to create a partial function. The functools module is for higher order functions. The partial() function syntax is:


partial(func, /, *args, **keywords)

It creates a partial function that behaves like calling “func” with fixed positional and keyword arguments. We only need to pass a few of the arguments required to call the underlying function. The other arguments are already provided in the *args and **kwargs.

Python Partial Function Example

Let’s say we have a multiply function like this:


def multiply(x, y):
    print(f'Arguments: {x}, {y}')
    return x * y

We want to utilize this function to multiply a list of integers with 4 and 5.


int_list = [1, 2, 3, 4]

int_list_4x = []

for i in int_list:
    int_list_4x.append(multiply(i, 4))

print(int_list_4x)

int_list_5x = []

for i in int_list:
    int_list_5x.append(multiply(i, 5))

print(int_list_5x)

Output:


Arguments: 1, 4
Arguments: 2, 4
Arguments: 3, 4
Arguments: 4, 4
[4, 8, 12, 16]
Arguments: 1, 5
Arguments: 2, 5
Arguments: 3, 5
Arguments: 4, 5
[5, 10, 15, 20]

We can create a partial function here to multiply an integer with 4 and 5.


times_4 = partial(multiply, 4)
times_5 = partial(multiply, 5)

Now, the updated code to multiply the list of integers with 4 and 5 will be like this:


int_list_4x = []

for i in int_list:
    int_list_4x.append(times_4(i))

print(int_list_4x)

int_list_5x = []

for i in int_list:
    int_list_5x.append(times_5(i))

print(int_list_5x)

We can further reduce the code size using list comprehension.


int_list_4x = [times_4(x) for x in int_list]

int_list_5x = [times_5(x) for x in int_list]

The output, in this case, will be like this:


Arguments: 4, 1
Arguments: 4, 2
Arguments: 4, 3
Arguments: 4, 4
[4, 8, 12, 16]
Arguments: 5, 1
Arguments: 5, 2
Arguments: 5, 3
Arguments: 5, 4
[5, 10, 15, 20]

Python Partial Function with keyword arguments

If you look at the above output, the positional argument of the partial function is always passed as the first parameter. In this case, it doesn’t matter. But, if the position of the partial function arguments matter, then we can use keyword arguments.


times_4 = partial(multiply, y=4)

int_list = [10, 20, 30]

int_list_4x = [times_4(x) for x in int_list]

print(int_list_4x)

Output:


Arguments: 10, 4
Arguments: 20, 4
Arguments: 30, 4
[40, 80, 120]

Conclusion

Python partial functions are useful in creating a separate function when we call a function multiple times with some argument being the same all the time.

References

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages