Python Advanced: How to use the Currying Method in Python

Home /

Table of Contents

Currying in Python

Currying is the method of converting the execution of a function that takes multiple arguments into a series of single-argument functions, which is useful in problem-solving and functional programming. Currying is used to convert functions with several parameters into functions with a single argument by assessing the incremental nesting of function arguments. While being executed, currying additionally fixes one parameter to another and creates a relative pattern.

Curry is a useful design pattern. Curry’s main goal is to turn a given function into a series of binding functions, which simplifies developers’ tasks and improves the readability of the solution. In this article, we have made an effort to explain how Curry functions and is implemented in Python.

How do design patterns work?

A design pattern offers a standardized response to a frequent or recurrent issue. The application of design patterns is very practical and aids programmers in improving the readability of the code they are writing.

Let’s first build a function with several arguments so that we may better grasp how currying is implemented. Take a look at the following function, which multiplies the supplied inputs.

Example:

def add(P, Q, R, S, T):
 summation = P + Q + R + S + T
 return summation
ans = add(11, 222, 333, 4444, 55555)
print(ans)
 

Output:

60565

Using Functions for Carrying

Binding the different parameters together is the first stage in the currying process. Consider a function with n arguments that need all of them bound. To do this, we fix the function with the first argument and build a new function that takes (n – 1) arguments. We now keep on constructing more functions until the function only accepts one argument.

Example:

def add(P):
   def add2(Q):
       def add3(R):
           def add4(S):
               def add5(T):
                   return  P + Q + R + S + T
               return add5
           return add4
       return add3
   return add2
 
ans = add(11)(222)(333)(4444)(55555)
print(ans)
 
 

Output:

60565

Currying Using Partial Function

Currying is a term used to describe a method in both mathematics and computer technology. Simply put, you derive new functions from existing ones that accept multiple arguments; these new functions, in turn, each accept a single parameter. Applying Partial Argument Application to the function you are generating with multiple parameters does this.

The partial() function, which is part of the functools built-in module, makes it easier to use a function’s partial application:

Example:

from functools import partial

def add(P, Q, R):
 return P+Q+R
ans = add(110, 220, 330)
print(ans)
 
add_110 = partial(add, 110)
add_120 = partial(add_110, 220)
print(add_120(330))

Output:

660
660

Currying Using Decorator

Using the decorator will make currying much more effective. A decorator enhances a function’s performance by enclosing it in code or functionality. We employ various common Python routines for that. (Click here for more information on decorators.)

To keep track of the number of parameters provided to the function, we first use a signature. Here, partial functions aid in the transformation of an n-parameter function into an n-parameter function.

Example:

from functools import partial
from inspect import signature
 
def curry(func):
 def inner(arg):
   if len(signature(func).parameters) == 1:
     return func(arg)
   return curry(partial(func, arg))
 return inner
 
@curry
def add(P, Q, R):
 return P + Q + R
print(add(110)(220)(330))

Output:

660
Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit
Other Recommended Article