Example analysis of partial usage of partial function in Python

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.