The examples in this paper describe the partial usage of partial functions in Python. Share to everyone for your reference. Specific as follows:
When the function is executed, it is called with all the necessary parameters. However, sometimes parameters can be learned in advance of the function being called. In this case, a function has one or more parameters that can be used in advance so that the function can be called with fewer parameters.
For example:
In [9]: from functools import Partialin [ten]: def add (A, B): ....: return a+b....:in [one]: Add (4,3) out[11]: 7In []: plus = Partial (add,100) in [+]: Plus (9) out[13]: 109In []: Plus2 = partial (add,99) in []: Plus2 (9) out[15]: 108
In fact, when the function is called, there are a number of parameter parameters, but one of the parameters has been known, we can use this parameter to rebind a new function, and then to call this new function.
If there are default parameters, they can also automatically correspond, for example:
in [+]: def add2 (a,b,c=2): ....: return a+b+c....:in []: Plus3 = Partail (add,101)------------------------------------ ---------------------------------------Nameerror Traceback (most recent)/users/yupeng/documents/phantomjs /in
()----> 1 PLUS3 = Partail (add,101) nameerror:name ' partail ' are not Definedin [+]: Plus 3 = partial (ADD,101) in []: Plus3 (1) out[20]: 102In [+]: Plus3 = partial (ADD2,101) in []: Plus3 = partial (add2,101) (1) O UT[22]: 104In [+]: Plus3 (1) out[23]: 104In []: Plus3 (out[24): 104In []: Plus3 (1,3) out[25]: 105In []: Plus3 (1,30) OUT[26]:
Hopefully this article will help you with Python programming.