Partial function, partial derivative
PythonFunctools
The module provides many useful functions, one of which is the Partial function ). Note that the partial function here is different from the partial function in the mathematical sense.
When introducing function parameters, we will talk about how to reduce the difficulty of function calling by setting the default values of parameters. This can also be done by partial functions.
Functools. partial
Is to help us create a partial function, we do not need to define our ownInt2 ()
, You can directly use the following code to create a new functionInt2
:
Import functoolsint2 = functools. partial (int, base = 2) int2 ('000000') # equivalent to int ('000000', base = 2)
When passed in:
Max2 = functools. partial (max, 10)
In fact10
As* Args
Is automatically added to the left, that is:
Max2 (5, 6, 7)
Equivalent:
Args = (10, 5, 6, 7) max (* args)
Summary
UseFunctools. partial
You can create a new function, which can fix some parameters of the original function, making it easier to call.