Python---partial function

Source: Internet
Author: User

# partial function Import functools# python's Functools module provides a number of useful functions, one of which is partial function (partial functions) # Here the partial function and the mathematical meaning of the partial function is different # by setting the default value of the parameter, Can reduce the difficulty of the function call, and the partial function can also do this the # int () function can convert a string to an integer, when only passing the string, the Int () function defaults to the decimal conversion print (int (' 12345 ') +1) # int () function also provides an additional base parameter, The default value is 10. If you pass in the base parameter, you can do the n-ary conversion print (int (' 12345 ', base=8)) print (int (' 12345 ', 16)) # If you want to convert a large number of binary strings, pass in int (x, base=2) each time Very troublesome, can define a function of Int2 (), by default base=2 passed in Def int2 (x, base=2): Return int (x, base) print (Int2 (' 1000000 ')) print (Int2 (' 1010101 ') ) # 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 Int2int2 = functools.partial (int, base=2) Print (Int2 (' 1000000 ')) print (Int2 (' 1010101 ')) # The function of the functools.partial is to fix some parameters of a function, return a new function, and call the new function easier # Notice the new Int2 function above, simply reset the base parameter to the default value of 2, but you can also pass in other values in the function call print (Int2 (' 1000000 ', base=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.