Skip to content
Home » Programming » Curried Functions in Python

Curried Functions in Python

Curried Functions in Python

In this post, we will investigate the details about the Curried Functions which are available in Python programming language. At first let us understand the concept of Currying.

Currying Concepts

Currying may be a technique which allows new functions to be created from existing functions by binding one or more parameters to a selected value. It is a serious source of reuse of functions in Python which suggests that functionality are often written once; in one place then reused in multiple other situations.

Consider having a function that takes two parameters. These two parameters; x and y are used within the function body with the multiply operator within the form x*y. For example,

mul(x, y): return x*y

This function mul() might then be used as follows;

total = mul(3, 5)

which would result in 5 being multiplied by 3 to give 15. It could also be used to double a number if needed; thus we could reuse the mul() function many times. For example,

mul(2, 5)
mul(2, 25)
mul(2, 50)

Above code snippets functions would double the second number. However, we have had to remember to provide the 2 so that the number can be doubled. However, the primary parameter (i.e. number 2) has not changed between any of the invocations of the mul() function.

What if we fixed the primary parameter to always be 2; this is able to mean that we could create a new function (replacement function) that apparently only takes one parameter (the number to double). For example,

double = mul(2, *)

such that we could now able to provide a single parameter as

double(5)
double(25)
double(50)

In essence double() is an alias for mul(); but an alias that provides the value 2 for the first parameter and leaves the second parameter to be filled in by the future invocation of the double() function.

Curried Functions in Python

A curried function in Python is a function where one or more of its parameters have been applied or bound to a value; resulting in the creation of a new function with one fewer parameters than the original. For example,

def mul(a, b):
    return a*b

The above code snippet simply multiplies any two numbers together. These numbers could be of any data type (integers or floating-point numbers, etc). We can thus invoke it in the normal manner as;

print (mul(3, 5))

The result of executing this statement is 15.

We could now define a new method that takes a function and a number and return a new function (anonymous) that takes one new parameter; and calls the function passed; with the number passed and the new parameter.

def mulby(function, number):
    return lambda y: function(number, y)

At this function, it has used the number passed into the mulby() function to the invocation of the function passed in; but it has also defined a new variable ‘y’ that will have to be provided when this new anonymous function is invoked. Then it returns a reference to the lambda (anonymous) function as the result of mulby.

The mulby() function can now be used to bind the first parameter of the mul() function to anything we want. For example,

quarter = mulby(mul, 4)

We could also bind different values to the first parameter of multiple to make a function as

penta = mulby(mul, 5)
hexa = mulby(mul, 6)
hepta = mulby(mul, 7)

which means we can now write

print(quarter(2))
print(penta(2))
print(hexa(2))
print(hepta(2))

which gives the following result

8
10
12
14

Curried functions are therefore very useful for creating new function from existing functions.

You can also check —->

Leave a Reply

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