Partial functions allow us to fix a certain number of arguments of a function and generate a new function.
In Python, a partial function is a function that has some of its arguments pre-set or partially applied. The partial function takes a function and one or more arguments and returns a new function that takes the remaining arguments.
The functools module in Python provides the partial function, which can be used to create partial functions. The partial function takes two or more arguments: the first argument is the function to be partially applied, and the remaining arguments are the arguments to be pre-set.
Here is an example:
from functools import partial def multiply(x, y): return x * y double = partial(multiply, y=2) print(double(5)) # Output: 10
In the above example, the partial
function is used to create a new function called double
. The multiply
function takes two arguments, but the partial
function sets the y
argument to 2. Therefore, when the double
function is called with an argument of 5, it calls multiply
with arguments 5
and 2
, and returns 10
.
Partial functions can be useful when you want to reuse a function with some of its arguments pre-set, or when you want to create a simpler version of a complex function by fixing some of its arguments.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.