Developer

Partial function application in Python

Python is not an inherently functional language, but with the help of the functools library you can write some programs in a functional style. One of the key tools do to writing functional code is partial function application, which is available through the functools module.

Python is not an inherently functional language, but with the help of the functools library you can write some programs in a functional style. One of the key tools do to writing functional code is partial function application, which is available through the functools module.

Partial application allows you to bind one of the inputs of a function to a constant value, and create a new function that takes only the remaining operations, and keeps the bound input the same among all calls. The following is a simple example which uses the partial function from the functools module with the add function from the operator module, which works in the same way as the "+" operator but can be used as an object.

>>> from functools import *
>>> from operator import *
>>> add(1,2)
3
>>> add1 = partial(add, 1)
>>> add1(2)
3
>>> add1(10)
11

Partial is what is called a higher order function, in that it takes a function as input (map and filter are other examples). The partial function is different in that it also returns a function that can be used in the same way as any other function in your program. You can use add1 exactly the same as any other function with one argument, you can apply it, use it as an argument to map or even partially apply it again:

>>> map(add1, [1,2,3,4,5])
[2, 3, 4, 5, 6]
>>> five = partial(add1, 4)
>>> five()
5

You can also use partial on object methods, for example to generate a list of defaults strings:

>>> standard = "User N"
>>> usern = partial(standard.replace, "N")
>>> usern("1")
'User 1'
>>> usern("1202")
'User 1202'

A less trivial example is adding items to widgets in a GUI with a callback. For the next example we'll be using the Tkinter toolkit, if you're not familiar with it, scan this previous howto. The following code sets up an application window and adds a listbox with a scrollbar:

from functools import *
from Tkinter import *

root = Tk()
root.geometry("600x400")

scrollbar = Scrollbar(root, orient=VERTICAL)
listbox = Listbox(root, yscrollcommand=scrollbar.set)

listbox.pack(side=LEFT, fill=BOTH, expand=1)
scrollbar.pack(side=RIGHT, fill=Y)

To add a string to the listbox, we use the following statement:

listbox.insert(END, "item")

This is a bit unwieldy, and can't be used as a callback, since it needs arguments. If you needed to add a lot of items to the list quickly, partially applying the listbox.insert method would let you write the following:

add = partial(listbox.insert, END)
add("item")

Alternatively, if you wanted to use it as a callback you would need to bind the string using partial as well:

additem = partial(listbox.insert, END, "item")
additem()

Partial function application is useful mainly in cases where you need to apply a range of different input to a single object, or need to bind one of the arguments to a function to be constant. It's not going to make or break your code, but it can make it easier to write, and easier to maintain.

Editor's Picks

Free Newsletters, In your Inbox