# Python Advanced: How to use the Currying Method in Python

Home /

## 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):
return  P + Q + R + S + T

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

return P+Q+R
print(ans)

```

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
return P + Q + R
```

Output:

``660``