partial method takes you to read from the source of functools

1.what?

  What partial Shi, partial, also called partial function. Source description is: given the new part of the application function parameters and keywords.

New function with partial application of the given arguments and keywords.

  

2.how?

  How to use it? Examples of official documents given as follows:

>>>from functools import partial
>>>basetwo = partial(int, base=2)

>>>basetwo.__doc__ 

'Convert base 2 string to an int.'

>>>basetwo('10010')

18

  The above examples may be somewhat at a loss you do not understand, then I explain to you

    1. First of all can understand why int, int can convert a number to a decimal number, where int takes two parameters: one is that we need to convert the number, the second is the need to convert the specified number is the number of decimal

    2. Then we passed the int and partical base = 2, What does that mean, that I have given you int specifies the number I want to pass is a binary number

    3. This last function is generated by the attachment to the basetwo, we only need to pass behind a number of features to basetwo can be achieved in an int

  Summary: predefined functions touch pass some parameters, only the remaining mass in the subsequent use of the parameters

 

3. Source resolve

Firstly, our partial () method is called __call__ partial, so we go directly to __call__ just fine

Very simple few lines of code, I believe we can understand, we ignore the first three lines, just look at the fourth and last lines will be able to understand, given the parameters and assign themselves to form a new args parameter, and finally return and a function of new parameters passed to the new method.

Guess you like

Origin www.cnblogs.com/liusijun113/p/11019243.html