Function Composition in Python
Last Updated :
03 Mar, 2025
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 programming, but Python's flexibility makes it simple to use function composition even though it's not a purely functional language.
In mathematical terms, if you have two functions F(x) and G(x), their composition is represented as F(G(x)). The output of G(x) is passed into F(x). In Python this means calling one function within another. For Example:
Python
# function to add 2 to a number
def add(x):
return x + 2
# function to multiply a number by 2
def mul(x):
return x * 2
# function composition f(g(x))
res = mul(add(5))
print(res)
Explanation: add() adds 2 to the input (5), resulting in 7 and mul() then multiplies 7 by 2, giving the final result 14.
Benefits of function composition
- Modularity: Encourages the use of smaller, single-responsibility functions. This helps break down complex logic into simpler, more manageable pieces.
- Reusability: Allows functions to be used in different contexts without rewriting logic.
- Readability: Clearly shows the flow of data.
- Avoiding Side Effects: Helps reduce unintended modifications of data.
A Better Approach to function composition
Rather than manually composing functions like in the above example, you can create a utility function that handles the composition of any two functions. This makes the process more flexible and reusable. Example:
Python
# function to compose two functions (f ∘ g)(x) = f(g(x))
def compose(f, g):
return lambda x: f(g(x))
# function to add 2 to a number
def add(x):
return x + 2
# function to multiply a number by 2
def mul(x):
return x * 2
# composite function
composed = compose(mul, add)
res = composed(5)
print(res)
Explanation: compose() takes two functions as arguments and returns a lambda function that applies g(x) first, then f(g(x)).
Composing Multiple Functions
The above method allows you to compose two functions, but what if you want to compose more than two? We can modify our utility function to handle multiple functions using Python’s reduce() function from the functools module. Example:
Python
from functools import reduce
# function to compose two functions
def composite_fun(*funcs):
def compose(f, g):
return lambda x: f(g(x))
return reduce(compose, funcs, lambda x: x)
# functions to add 2, subtract 1, and multiply by 2
def add(x):
return x + 2
def mul(x):
return x * 2
def sub(x):
return x - 1
# compose the functions
composed_fn = composite_fun(mul, sub, add)
# apply the composed function
res = composed_fn(5)
print(res)
Explanation: reduce() applies the compose function across multiple functions sequentially. The execution order is add(5) → sub(7) → mul(6), resulting in 12.
Handling multiple arguments in function composition
Function composition typically expects each function to take a single input. However, if your functions need to handle multiple arguments, you can adjust the composition function to handle tuples or other structures.
Example 1:
Python
# function to multiply both x and y by 2
def process_input(x, y):
return x * 2, y * 2
# function to sum x and y
def sum_inputs(args):
x, y = args
return x + y
# compose functions to handle multiple arguments
def composed_fun(x, y):
return sum_inputs(process_input(x, y))
res = composed_fun(3, 4)
print(res)
Explanation: process_input doubles both x and y, returning (6, 8) and sum_inputs adds 6 + 8, resulting in 14.
Example 2: Composing with different return types
Python
# Function to compose two functions (f ∘ g)(x) = f(g(x))
def compose(f, g):
def inner(x):
return f(g(x))
return inner
def greet(name):
return f"Hello, {name}!"
def uppercase(greeting):
return greeting.upper()
composed_fun = compose(uppercase, greet)
# Apply the composed function
res = composed_fun("shakshi")
print(res)
Explanation: greet("shakshi") returns "Hello, shakshi!" and uppercase converts it to "HELLO, SHAKSHI!" .
Function composition with decorators
In Python, decorators provide a way to modify or enhance functions or methods. Function composition is also a common technique used in decorators to combine multiple behaviors into a single function. Example:
Python
# decorators for logging and timing
def log_function(func):
def wrapper(*args, **kwargs):
print(f"Calling {func.__name__} with arguments {args} and keyword arguments {kwargs}")
return func(*args, **kwargs)
return wrapper
def time_function(func):
import time
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(f"Execution time: {end - start} seconds")
return result
return wrapper
# composing decorators
@log_function
@time_function
def add(x, y):
return x + y
# calling the decorated function
res = add(3, 4)
print(res)
OutputCalling wrapper with arguments (3, 4) and keyword arguments {}
Execution time: 1.430511474609375e-06 seconds
7
Explanation: log_function logs function calls and arguments and time_function measures execution time. The add function is wrapped with both decorators, demonstrating function composition in decorators.
Similar Reads
Function Annotations in Python
Basic Terminology PEP: PEP stands for Python Enhancement Proposal. It is a design document that describes new features for Python or its processes or environment. It also provides information to the python community. PEP is a primary mechanism for proposing major new features, for example - Python W
7 min read
Python compile() Function
Python is a high-level, general-purpose, and very popular programming language. In this article, we will learn about the Python compile() function. Python compile() Function SyntaxPython compile() function takes source code as input and returns a code object that is ready to be executed and which ca
3 min read
cmp() function - Python
cmp() method in Python 2.x compares two integers and returns -1, 0, 1 according to comparison. cmp() does not work in python 3.x. You might want to see list comparison in Python.ExamplePython# Example 1 print(cmp(3, 4)) # Example 2 print(cmp(5, 5)) # Example 3 print(cmp(7, 6)) Output-101ExplanationI
3 min read
Currying Function in Python
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 IllustrationIn currying, a function that takes multiple inputs is broken down into a series of single-argument functions, ea
3 min read
dir() function in Python
The dir() function is a built-in Python tool used to list the attributes (like methods, variables, etc.) of an object. It helps inspect modules, classes, functions, and even user-defined objects during development and debugging.Syntaxdir([object])Parameters: object (optional): Any Python object (lik
3 min read
numpy.fromstring() function â Python
numpy.fromstring() function create a new one-dimensional array initialized from text data in a string. Syntax : numpy.fromstring(string, dtype = float, count = -1, sep = ' ') Parameters : string : [str] A string that contained the data. dtype : [data-type, optional] Data-type of the array. Default d
1 min read
chr() Function in Python
chr() function returns a string representing a character whose Unicode code point is the integer specified. chr() Example: Python3 num = 97 print("ASCII Value of 97 is: ", chr(num)) OutputASCII Value of 97 is: a Python chr() Function Syntaxchr(num) Parametersnum: an Unicode code integerRet
3 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
Wand function() function in Python
function() function is similar to evaluate function. In function() function pixel channels can be manipulated by applies a multi-argument function to pixel channels. Following are the list of FUNCTION_TYPES in Wand: 'undefined''arcsin''arctan''polynomial''sinusoid' Syntax : wand.image.function(funct
1 min read
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