DEV Community

Cover image for How to Use Partial Function in Python
Ankit malik
Ankit malik

Posted on

How to Use Partial Function in Python

Introduction

In Python, functions are first-class objects, which means they can be passed around, modified, or even treated as variables. The functools module provides a tool called partial function, which allows you to fix a certain number of arguments of a function and generate a new function with fewer parameters. This is especially useful when working with repetitive function calls where many arguments remain the same.

Use Cases

Partial functions are commonly used in scenarios where:

  • Pre-filling function parameters: You want to reuse a function with some arguments fixed, avoiding repeated input.
  • Simplifying callbacks: Passing simplified functions to APIs, GUIs, or event-driven frameworks.
  • Improving code readability: Defining specialized versions of general-purpose functions, such as formatting or mathematical calculations.
  • Data processing pipelines: When mapping or filtering with functions that require multiple arguments, a partial function can simplify the structure.

Code Example with Explanation

Let’s explore an example.

from functools import partial # A normal function that multiplies two numbers def multiply(x, y): return x * y # Creating a partial function to always multiply with 2 double = partial(multiply, 2) # Creating another partial function to always multiply with 5 five_times = partial(multiply, 5) # Using the partial functions print(double(10)) # Output: 20 (2 * 10) print(five_times(4)) # Output: 20 (5 * 4) 
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The function multiply(x, y) takes two arguments.
  • By applying partial(multiply, 2), we fix x = 2. The new function double(y) only needs one argument.
  • Similarly, five_times(y) will always multiply numbers by 5.
  • This approach eliminates rewriting separate functions for these common operations.

Another real-world example with formatting:

from functools import partial def format_text(text, prefix, suffix): return f"{prefix}{text}{suffix}" # Create specific formatters using partial functions add_brackets = partial(format_text, prefix="[", suffix="]") add_quotes = partial(format_text, prefix='"', suffix='"') print(add_brackets("Python")) # Output: [Python] print(add_quotes("Hello")) # Output: "Hello" 
Enter fullscreen mode Exit fullscreen mode

This technique is particularly handy when creating specialized behaviors without redefining functions.

Conclusion

Partial functions in Python, provided by the functools module, are a clean and efficient way to reduce code duplication. By fixing certain arguments of a function, they let you create customized versions of existing functions for specific use cases. Whether for callbacks, formatting, or data processing, partial functions help write cleaner and more reusable code.

Top comments (6)

Collapse
 
xwero profile image
david duymelinck • Edited

You don't need functools, just create a function with an inner function.

def make_multiplier(x): def multiplier(n): return x * n return multiplier 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ankitmalikg profile image
Ankit malik

You might also need that multiplier function at all, it's just an easy example for understanding.

Partial function is way in which you can create a new function by passing partial values.

Collapse
 
fnlsamo profile image
ApolloMDjr

Thanks

Collapse
 
a-k-0047 profile image
ak0047

Thank you for sharing this article.
This module is new to me, and I’d like to learn more about what functools can do.

Collapse
 
raziq_din_bd0274cc2ac748b profile image
Raziq Din

Great Read!

Collapse
 
rafsun_jany profile image
Mohammad Rafsun Jany Mahin

Helpful, thanks