Python Partial function

Source: Internet
Author: User

The Int function converts a string to an integer, and the Int () function defaults to the decimal conversion when the string is passed in only:

Print  int ("12345")    #12345

The INT function also provides an additional base parameter, with a default value of 10, which can be converted into n-ary if the base parameter is passed in

Print  int ("12345", base=8)  #5349print int ("12345", +)   #74565

Assuming that you want to convert a large number of binary strings, it's very troublesome to pass in int (x, base=2) every time, so we think that we can define a function of Int2 (), by default base=2 it in.

def  Int2 (x,base=2):    return  int (x,base) print  int2 ("10000000")   #128print  int2 ("1010101" )     #85

By setting the default value of the parameter, you can reduce the difficulty of the function call. While the partial function can do this, the Functools module provides a number of useful functions, one of which is the partial function
Functools.partial is to help us create a biased function, we do not need to define INT2 (), you can directly use the following code to create a new function Int2:

Import  functoolsint2=functools.partial (int,base=2) print  int2 ("10000000")   #128print  Int2 (" 1010101 ")     #85

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.

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:

Print  int2 (' 1000000 ', base=10)  #1000000

When you create a partial function, you can accept the three parameters of the function object, *args, **kw

Int2 = functools.partial (int, base=2) actually fixed the keyword argument base of the INT function, which is

Int2 ("10010") is equivalent to:
Kw={base:2}
int{' 10010 ', **kw}

When incoming:

Max2=functools.partial (max,2)

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)

Transferred from: https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 00143184474383175eeea92a8b0439fab7b392a8a32f8fa000

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.