10 minutes a day, 12 days Getting Started Python (vi)

Source: Internet
Author: User

Partial Function- when a function has too many arguments to simplify, use the functools.partial You can create a new function that can fix some of the parameters of the original function, making it easier to invoke.

Import Functools


Int2 = functools.partial (int,base=2)

print int (' 1000000 ', 2)

# 64

Print Int2 (' 1000000 ')

# 64

A simple summary of the role of functools.partial is to fix certain parameters of a function (that is, set default values), return a new function, and make it easier to call the new function.

Notice that the new Int2 function above simply re-sets the base parameter to the default value of 2, but can also pass in other values when the function is called:

Int2 (' 1000000 ', base=10)

1000000

To create a partial function, the right-to-left fixed parameter, that is, for the function f (A1, A2, A3), can be fixed A3, can also be fixed A3 and A2, but also fixed a3,a2 and A1, but do not jump fixed, such as only fixed A1 and A3, the A2 leaked. If you do this, calling the new function is more complicated and you can try it yourself.

10 minutes a day, 12 days Getting Started Python (vi)

Related Article

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.