Python functional programming-biased functions, python Functions

Source: Internet
Author: User

Python functional programming-biased functions, python Functions
PythonfunctoolsThe module provides many useful functions, one of which is the Partial function ). Note that the partial function here is different from the partial function in the mathematical sense.

When introducing function parameters, we will talk about how to reduce the difficulty of function calling by setting the default values of parameters. This can also be done by partial functions. Example:

int()The function can convert a string to an integer. when only a string is input,int()By default, the function is converted in decimal format:

>>> int('12345')12345

Howeverint()The function also provides additionalbaseParameter. The default value is10. IfbaseTo convert the N-base:>>> int('12345', base=8

5349>>> int('12345', 16)74565

Assume that you want to convert a large number of binary strings.int(x, base=2)So we thought we could defineint2()By defaultbase=2Input:

def int2(x, base=2):    return int(x, base)

In this way, binary conversion is very convenient:

>>> int2('1000000')64>>> int2('1010101')85

functools.partialIs to help us create a partial function, we do not need to define our ownint2(), You can directly use the following code to create a new functionint2:

>>> import functools>>> int2 = functools.partial(int, base=2)>>> int2('1000000')64>>> int2('1010101')85

Therefore, a brief summaryfunctools.partialThe function is to fix some parameters of a function (that is, to set the default value) and return a new function. It is easier to call this function.

Notice the newint2Function, just putbaseReset the default value of the parameter2But you can also input other values when calling the function:

>>> int2('1000000', base=10)1000000

Finally, when creating partial functions, you can actually receive function objects,*argsAnd**kwWhen these three parameters are passed in:

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

Actually, the keyword parameter of the int () function is fixed.base, That is:

int2('10010')

Equivalent:

kw = { 'base': 2 }int('10010', **kw)

When passed in:

max2 = functools.partial(max, 10)

In fact10As*argsIs automatically added to the left, that is:

max2(5, 6, 7)

Equivalent:

args = (10, 5, 6, 7)max(*args)

Result:10.

Summary

Usefunctools.partialYou can create a new function, which can fix some parameters of the original function, making it easier to call.

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.