Currying Function in Python
Last Updated :
27 Feb, 2025
Currying is a functional programming technique where a function with multiple arguments is transformed into a sequence of functions, each taking a single argument.
Mathematical Illustration
In currying, a function that takes multiple inputs is broken down into a series of single-argument functions, each processing one input at a time and ultimately returning the final result. For example:
f(x, y) = (x³) + (y³)
h(x) = x³
h(y) = y³
f(x, y) = h(x) + h(y)
Notice that, the use of currying avoids unnecessary repetition and makes the transformation clearer.
Let’s create a composite function where a(x) = b(c(d(x))), meaning that function d(x) is executed first, its output is passed to c(x) and finally, b(x) processes the result.
Python
# Function to create a composite function using currying
def change(b, c, d):
def a(x):
return b(c(d(x)))
return a
# Example functions for transformation
def b(num):
return num + 5
def c(num):
return num * 2
def d(num):
return num - 3
# Creating a composed function
fun = change(b, c, d)
print(fun(10))
Explanation: change function takes three functions (b, c, d) and applies them in such order that the output of function d is passed to function c and then the output of c is passed to function b and the final output is stored in fun.
Lets discuss some of common use cases of Currying in Python with examples.
Unit Conversion
Let's try to convert kilometers to meters, then to centimeters and finally to feet using function composition, ensuring a structured, readable and reusable approach.
Python
def change(b, c, d):
def a(x):
return b(c(d(x)))
return a
def km_to_m(dist):
""" Function that converts km to m. """
return dist * 1000
def m_to_cm(dist):
""" Function that converts m to cm. """
return dist * 100
def cm_to_f(dist):
""" Function that converts cm to ft. """
return dist / 30.48
transform = change(cm_to_f, m_to_cm, km_to_m)
print(transform(565))
Days To Seconds Conversion
In this example, we’ll convert time from days to hours, then to minutes and finally to seconds using function composition.
Python
def change(b, c, d):
def a(x):
return b(c(d(x)))
return a
def d_to_h(time):
""" Function that converts days to hours. """
return time * 24
def h_to_m(time):
""" Function that converts hours to minutes. """
return time * 60
def m_to_s(time):
""" Function that converts minutes to seconds. """
return time * 60
transform = change(m_to_s, h_to_m, d_to_h)
print(transform(10))
Event Handling in GUI Applications
In GUI programming, event handlers are functions that respond to user actions such as button clicks, mouse movements or key presses. Currying can be useful in these scenarios by allowing us to predefine some arguments for event-handling functions while keeping the remaining arguments flexible. Lets discuss with an example of Button Click Handler in Python:
Python
def button_click(action):
def execute(event):
print(f"Button clicked! Performing {action}")
return execute
click_handler = button_click("Save")
click_handler(None)
OutputButton clicked! Performing Save
Explanation:
- This function takes an action (e.g., "Save" or "Delete") as input.
- It returns a new function (execute), which prints a message when called.
- The event parameter in execute(event) ensures compatibility with GUI frameworks like Tkinter, which pass event objects when calling event handlers.
Similar Reads
Python Built in Functions Python is the most popular programming language created by Guido van Rossum in 1991. It is used for system scripting, software development, and web development (server-side). Web applications can be developed on a server using Python. Workflows can be made with Python and other technologies. Databas
6 min read
Function aliasing in Python In Python, we can give another name of the function. For the existing function, we can give another name, which is nothing but function aliasing. Function aliasing in Python In function aliasing, we create a new variable and assign the function reference to one existing function to the variable. We
2 min read
Python int() Function The Python int() function converts a given object to an integer or converts a decimal (floating-point) number to its integer part by truncating the fractional part.Example:In this example, we passed a string as an argument to the int() function and printed it.Pythonage = "21" print("age =", int(age)
3 min read
Function Composition in Python Function composition is a powerful concept where two or more functions are combined in such a way that the output of one function becomes the input for the next. It allows for the creation of complex functionality from simple, reusable building blocks. This concept is widely used in functional progr
6 min read
Python 3 - input() function In Python, we use the input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function converts it into a string.Python input() Function SyntaxSyntax: input(prompt)Parameter:Prompt: (optional
3 min read
sum() function in Python The sum of numbers in the list is required everywhere. Python provides an inbuilt function sum() which sums up the numbers in the list. Pythonarr = [1, 5, 2] print(sum(arr))Output8 Sum() Function in Python Syntax Syntax : sum(iterable, start) iterable : iterable can be anything list , tuples or dict
3 min read
set() Function in python set() function in Python is used to create a set, which is an unordered collection of unique elements. Sets are mutable, meaning elements can be added or removed after creation. However, all elements inside a set must be immutable, such as numbers, strings or tuples. The set() function can take an i
3 min read
First Class functions in Python First-class function is a concept where functions are treated as first-class citizens. By treating functions as first-class citizens, Python allows you to write more abstract, reusable, and modular code. This means that functions in such languages are treated like any other variable. They can be pas
2 min read
type() function in Python The type() function is mostly used for debugging purposes. Two different types of arguments can be passed to type() function, single and three arguments. If a single argument type(obj) is passed, it returns the type of the given object. If three argument types (object, bases, dict) are passed, it re
5 min read