Applying partial arguments with partial()

The partial() function leads to something called a partial application. A partially applied function is a new function built from an old function and a subset of the required arguments. It is closely related to the concept of currying. Much of the theoretical background is not relevant here, since currying doesn't apply to the way Python functions are implemented. The concept, however, can lead us to some handy simplifications.

We can look at trivial examples as follows:

>>> exp2 = partial(pow, 2)>>> exp2(12)4096>>> exp2(17)-1131071

We've created the function exp2(y), which is the pow(2, y) function. The partial() function bounds the first positional parameter to the pow() function. When we evaluate ...

Get Functional Python Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.