Open In App

Partial Functions in Python

Last Updated : 21 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Partial functions allow us to fix a certain number of arguments of a function and generate a new function. In this article, we will try to understand the concept of Partial Functions with different examples in Python.

What are Partial functions and the use of partial functions in Python?

Partial functions in Python is a function that is created by fixing a certain number of arguments of another function. Python provides a built-in module called functools that includes a function called partial that can be used to create partial functions. The partial function takes a callable (usually another function) and a series of arguments to be pre-filled in the new partial function. This feature is similar to bind in C++.

How do you implement a partial function in Python?

Partial functions support both positional and keyword arguments to be used as fixed arguments.

Example 1

In this example, we use default values to implement the partial function. The default values start replacing variables from the left. In the example, we have pre-filled our function with some constant values of a, b, and c. And g() just takes a single argument i.e. the variable x.

Python3




from functools import partial
 
# A normal function
def f(a, b, c, x):
    return 1000*a + 100*b + 10*c + x
 
# A partial function that calls f with
# a as 3, b as 1 and c as 4.
g = partial(f, 3, 1, 4)
 
# Calling g()
print(g(5))


Output:

3145

Example 2

In the example, we have used pre-defined value constant values in which we have assigned the values of c and b and add_part() takes a single argument i.e. the variable a.

Python3




from functools import *
 
# A normal function
def add(a, b, c):
    return 100 * a + 10 * b + c
 
# A partial function with b = 1 and c = 2
add_part = partial(add, c = 2, b = 1)
 
# Calling partial function
print(add_part(3))


Output:

312

Uses of partial functions

  • Integration with Libraries: Partial functions can be used to customize the behavior of third-party functions or methods by providing partial arguments and can be used to integrate it with other libraries.
  • Simplifying Callbacks: Partial functions can be used to create specialized callback handlers by fixing some callback-specific parameters and providing a cleaner interface for the rest of the code.
  • Parameter Fixing: : Partial functions can be very useful when we have a function with multiple parameters and we frequently want to use it with some parameters fixed. Instead of repeatedly passing those fixed parameters, we can create a partial function and call it with the remaining arguments.
  • Reducing Duplication: If we are using the same arguments for a function in various places, creating a partial function with those fixed arguments can help to reduce code duplication and maintenance efforts.
  • Default Arguments: Python’s built-in functools.partial can be used to set default values for function arguments.
  • Reusability of code: Partial functions can be used to derive specialized functions from general functions and therefore help us to reuse our code.



Similar Reads

NLP | Partial parsing with Regex
Defining a grammar to parse 3 phrase types. ChunkRule class that looks for an optional determiner followed by one or more nouns is used for noun phrases. To add an adjective to the front of a noun chunk, MergeRule class is used. Any IN word is simply chunked for the prepositional phrases. an optional modal word (such as should) followed by a verb i
2 min read
Partial Orders and Lattices (Set-2) | Mathematics
Prerequisite: Partial Orders and Lattices | Set-1 Well Ordered Set - Given a poset, (X, ≤) we say that ≤ is a well-order (well-ordering) and that is well-ordered by ≤ if every nonempty subset of X has a least element. When X is non-empty, if we pick any two-element subset, {a, b}, of X, since the subset {a, b} must have a least element, we see that
6 min read
Partial JSON Update Using SQLAlchemy Expression
In this article, we will learn how to use SQLAlchemy expression to update partial JSON in Python. SQLAlchemy is a Python library that provides a set of high-level abstractions for working with relational databases. It allows developers to work with databases in a more Pythonic way, by providing an object-relational mapping (ORM) layer that maps Pyt
5 min read
Mathematical Functions in Python | Set 1 (Numeric Functions)
In python a number of mathematical operations can be performed with ease by importing a module named "math" which defines various functions which makes our tasks easier. 1. ceil() :- This function returns the smallest integral value greater than the number. If number is already integer, same number is returned. 2. floor() :- This function returns t
3 min read
Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
Numeric functions are discussed in set 1 below Mathematical Functions in Python | Set 1 ( Numeric Functions) Logarithmic and power functions are discussed in this set. 1. exp(a) :- This function returns the value of e raised to the power a (e**a) . 2. log(a, b) :- This function returns the logarithmic value of a with base b. If base is not mentione
3 min read
Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
Some of the mathematical functions are discussed in below set 1 and set 2 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Trigonometric and angular functions are discussed in this article. 1. sin() :- This function returns the sine of value passed as argument. T
3 min read
Mathematical Functions in Python | Set 4 (Special Functions and Constants)
Some of the mathematical functions are discussed in below set 1, set 2 and set 3 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions) Special Functions and constants are discussed in this
2 min read
Time Functions in Python | Set 1 (time(), ctime(), sleep()...)
Python has defined a module, "time" which allows us to handle various operations regarding time, its conversions and representations, which find its use in various applications in life. The beginning of time started measuring from 1 January, 12:00 am, 1970 and this very time is termed as "epoch" in Python. Operations on Time in Python Python time.t
4 min read
Array in Python | Set 2 (Important Functions)
Array in Python | Set 1 (Introduction and Functions)Array in Python | Set 2Below are some more useful functions provided in Python for arrays: Array Typecode FunctionThis function returns the data type by which the array is initialized. In this example, we are using arr.typecode to find out the data type of array initialization. C/C++ Code # import
3 min read
Bisect Algorithm Functions in Python
The purpose of Bisect algorithm is to find a position in list where an element needs to be inserted to keep the list sorted. Python in its definition provides the bisect algorithms using the module "bisect" which allows keeping the list in sorted order after the insertion of each element. This is essential as this reduces overhead time required to
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg