DEV Community

Periklis Gkolias
Periklis Gkolias

Posted on

A Primer on Python Partial Functions

Partial functions are one of the most "terrifying concepts" I hear people talking about. Personally, I think the idea is fairly easy to grasp. That is, I will try to explain in a few paragraphs with python and help you escape the math labyrinth of "official documentation".

What is a partial function

Partial functions are a computer science concept where we can derive a function with x parameters to a function with fewer parameters and constant values set for the more limited function.

They became more mainstream, as functional programming arose and the main pillars of it (map, filter, reduce, etc), require functions as an argument.

Let's break this down

An example

Searching the official python documentation I found this example:

>>> from functools import partial
>>> basetwo = partial(int, base=2)
>>> basetwo.__doc__ = 'Convert base 2 string to an int.'
>>> basetwo('10010')
Enter fullscreen mode Exit fullscreen mode

So, we have function int which, as you can imagine, takes an argument (number or string) and converts it to an integer (if technically possible). This function also takes various arguments. One of them is base.

Don't worry about the __doc__ thing, it is the message that is printed when you asked for the documentation of the function.

So in the example above, we invoke the partial factory, to derive a new (more specialized) function from the int one.

Why don't we just use default arguments?

Interesting question, thanks for asking. The difference between the example above and your proposal is that calling
int('110', base=2) will return an integer or in general the result of the execution of the function.

basetwo on the other hand, is a function reference, which in turn you can use it as input for more partial functions or to functions that get other functions as arguments eg map.

So is this like overloading and overriding?

Overloading is when you have one function, with the same name but a different signature. When you create a partial function, you give the function a different name (unless you want for some reason to shadow the original one).

Overriding, on the other hand, refers to a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its super-classes. Partial functions provide specialized versions of initial functions but that usually is under a different name and for sure it is not bound to inheritance trees

I see, so it is just a function that modifies functions on the fly

You can say it this way, too.

Based on the python examples again, this is how the partial factory would look like:

def partial(func, *args, **keywords):
    def newfunc(*fargs, **fkeywords):
        newkeywords = keywords.copy()
        return func(*args, *fargs, **newkeywords)
    newfunc.func = func
    newfunc.args = args
    newfunc.keywords = keywords
    return newfunc
Enter fullscreen mode Exit fullscreen mode

Given a function(func) and a set of values(*args, **keywords), it creates a new function (newfunc) which performs some business logic by invoking the passed function (func) and potentially using *args, **keywords.


Thank you for reading this short article and I hope it was helpful to you. Let me know your thoughts in the comments.

Top comments (0)