Python partial function

Source: Internet
Author: User

Partial function

The function of functools.partial is to fix certain parameters of a function (that is, to set the default value
def int2 (x, base=2):
return int (x, Base)

>>> Import Functools
>>> Int2 = functools.partial (int, base=2)

When a function has too many arguments and needs to be simplified, use functools.partial to create a new function that can fix some of the parameters of the original function, making it easier to call

Max2 = Functools.partial (max, 10)
Will actually automatically add 10 as part of the *args to the left, namely:

MAX2 (5, 6, 7)
Equivalent:
args = (10, 5, 6, 7)
Max (*args)
The result is 10.

Python 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.