# partial function Import functools# python's Functools module provides a number of useful functions, one of which is partial function (partial functions) # Here the partial function and the mathematical meaning of the partial function is different # by setting the default value of the parameter, Can reduce the difficulty of the function call, and the partial function can also do this the # int () function can convert a string to an integer, when only passing the string, the Int () function defaults to the decimal conversion print (int (' 12345 ') +1) # int () function also provides an additional base parameter, The default value is 10. If you pass in the base parameter, you can do the n-ary conversion print (int (' 12345 ', base=8)) print (int (' 12345 ', 16)) # If you want to convert a large number of binary strings, pass in int (x, base=2) each time Very troublesome, can define a function of Int2 (), by default base=2 passed in Def int2 (x, base=2): Return int (x, base) print (Int2 (' 1000000 ')) print (Int2 (' 1010101 ') ) # functools.partial is to help us create a biased function, we do not need to define INT2 (), you can directly use the following code to create a new function Int2int2 = functools.partial (int, base=2) Print (Int2 (' 1000000 ')) print (Int2 (' 1010101 ')) # The function of the functools.partial is to fix some parameters of a function, return a new function, and call the new function easier # Notice the new Int2 function above, simply reset the base parameter to the default value of 2, but you can also pass in other values in the function call print (Int2 (' 1000000 ', base=10))
Python---partial function