Python Partial Functions
Category Programming Techniques
Python partial functions are invoked by users through the functools module.
Application of Partial Functions
When a function is executed, it must be called with all the necessary arguments. However, sometimes the arguments can be known before the function is called. In this case, one or more arguments of a function can be predetermined, allowing the function to be called with fewer arguments.
A partial function takes the function to be carried as the first argument of the partial() function, and the arguments of the original function are sequentially passed as subsequent arguments of the partial() function, unless keyword arguments are used.
It may not be understood how partial functions are used just by description, so let's take a common example to illustrate. In this example, we implement a modulo function, which takes the remainder of 100%m for different numbers m.
from functools import partial
def mod( n, m ):
return n % m
mod_by_100 = partial( mod, 100 )
print mod( 100, 7 ) # 2
print mod_by_100( 7 ) # 2
Since the examples usually choose addition or multiplication for explanation, it is difficult to appreciate the positional issue of partial function parameters, which can easily give people the illusion that the second argument of partial is also the second argument of the original function. Therefore, I choose mod to explain here.
In the case of keyword arguments, you can ignore the order and number of the original function's parameters. Let's look at another example, which explains how to perform different base conversions.
from functools import partial
bin2dec = partial( int, base=2 )
print bin2dec( '0b10001' ) # 17
print bin2dec( '10001' ) # 17
hex2dec = partial( int, base=16 )
print hex2dec( '0x67' ) # 103
print hex2dec( '67' ) # 103
These applications of partial functions seem simple, but they are very useful and can effectively implement the DRY principle, saving programming costs.
>
From Pythoner
Article link: http://www.pythoner.com/54.html
#
-
** Praise
* 103**[email protected]
Regarding partial functions, they can enhance existing functions.
>>> import functools
>>> int2 =functools.partial (int, base=2) # The conversion of int is set to binary, where base is the parameter indicating the base of the int function.
>>>int2('1000000')
64
>>>int2('1010101')
85
After resetting, you can also pass other values during the function call:
>>> int2('1000000', base=10) # Here the base becomes 10, overriding the default value of 2.
1000000
Note that after creating a new partial function, you can still change the default value that has been set, but it must be clearly stated that the value of base has been changed.
Otherwise, if you directly pass a number, an error will occur:
int2('100', 10) # Error, 10 before is not added as base=, it cannot be distinguished as being passed to base
The reason is as follows:
When creating a partial function, it can actually receive three arguments: function objects, args, and *kw. When passed:
int2 =functools.partial(int, base=2) # int is the function object, base=2 is **kw, no *args argument is passed
In fact, it fixes the keyword argument base of the int() function, that is:
int2('10010')
It is equivalent to:
kw = { 'base': 2 }
int('10010', **kw) # If base=2 is not specified and only 2 is passed, then 2 is considered as the value of *args
When passed:
max2 =functools.partial(max, 10) # Here 10 is obviously passed as a value in *args
In fact, it will automatically add 10 as part of *args to the argument list, that is:
``` max2(5,6, 7) # Originally there