Python Partial functions Partial function

Source: Internet
Author: User

1 Import Functools 2 int2 = functools.partial (int, base=2)3print('int2 '  ))456 max2 = functools.partial (max, ten)7 Print (Max2 (2,6,0,8))

  The function of functools.partial is to fix certain parameters of a function (that is, to set default values), to return a new function, and to call this new function is simpler.

When you create a partial function, you can actually receive the 3 parameters of the function object, *args, and **kw.

In the preceding code, base=2 is equivalent to fixing the keyword parameter base in the Int () function to 2

The max () function, in effect, automatically adds 10 as part of the *args to the left.

When the function parameter is too long, a new function can be encapsulated by functools.partial.

Python Partial functions Partial function

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.