A detailed introduction to the partial function _python in Python

Source: Internet
Author: User

Python's Functools module provides a number of useful features, one of which is the partial function (Partial functions). Note that the partial function here is not the same as the partial function in mathematical sense.

When we introduce the function parameters, we can reduce the difficulty of the function call by setting the default value of the parameter. And the partial function can also do this. Examples are as follows:

the int () function converts a string to an integer, and the int () function is converted to decimal by default when only the string is passed in:

>>> int (' 12345 ')
12345

However, the Int () function also provides an additional base parameter with a default value of 10. If you pass in the base parameter, you can do an n-ary conversion:

>>> int (' 12345 ', base=8)
5349
>>> int (' 12345 ', 74565)


Suppose that to convert a large number of binary strings, passing in int (x, base=2) is very troublesome, so we think that we can define a int2 () function, by default, base=2 is passed in:

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

This makes it very convenient for us to convert the binaries:

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

Functools.partial is to help us create a partial function, we do not need to define INT2 (), you can create a new function directly using the following code Int2:

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

So, a simple summary of Functools.partial's role is to put some of the parameters of a function to the fixed (that is, set the default value), return a new function, call this new function is simpler.

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

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

Finally, when you create a partial function, you can actually receive the 3 parameters of the function object, *args, and **kw when you pass in:

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

Actually fixed the keyword parameter base for the int () function, which is:

Int2 (' 10010 ')

Equivalent:

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

When incoming:

Max2 = Functools.partial (max, 10)

In fact, the 10 is automatically added to the left as part of the *args, which is:

MAX2 (5, 6, 7)

Equivalent:

args = (5, 6, 7)
Max (*args)

The result is 10.
Summary

When the number of arguments for a function is too much to simplify, use functools.partial to create a new function that can hold some of the parameters of the original function, making it simpler to invoke.

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.