Open In App

Function Composition in Python

Last Updated : 03 Mar, 2025
Comments
Improve
Suggest changes
6 Likes
Like
Report

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:


Output
14

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:


Output
14

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:


Output
12

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:


Output
14

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


Output
HELLO, SHAKSHI!

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:


Output
Calling 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.


Next Article
Article Tags :
Practice Tags :

Similar Reads