Use of the Python partial function

Source: Internet
Author: User
partial function in Python

When a function has many parameters, the caller needs to supply multiple arguments. If you reduce the number of parameters, you can simplify the burden on the caller.

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

>>> 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, functools.partial can turn a function with many parameters into a new function with fewer parameters, and fewer parameters need to specify a default value at the time of creation, so the difficulty of the new function call is reduced. Task

In the 7th section, we can implement a case-insensitive sort by passing in the custom sort function in sorted, a higher-order function. Please use functools.partial to turn this complex call into a simple function:

Sorted_ignore_case (iterable)

To fix the CMP parameter for sorted (), you need to pass in a sort function to be the default value for CMP.

Reference code:

#!/usr/bin/python
#coding: utf-8

functools

# cmp = lambda S1, s2:cmp (S1.upper (), S2.upper ()) The leftmost must have CMP =, so that when you execute print, you will perform the
CMP function in the # anonymous function, and for why CMP =, see the example above, base = 2, if there is no base =, the result
# will definitely go wrong
# CMP Letter Number interpretation:
the # CMP (x,y) function is used to compare 2 objects if x < y returns 1, if x = = y returns 0, if x > y returns 1
# for sorting, default from small to large
 S1, s 2:CMP (S1.upper (), S2.upper ()))
print(Sorted_ignore_case ([' Bob ', ' about ', ' Zoo ', ' Credit '])


When you do not use a partial function
'
def cmp_ignore_case (S1, S2):
    u1 = S1.upper ()
    U2 = S2.upper ()
    if U1 > U2: Return
        1
    if U1 < u2:
        return-1 return
    0

print sorted ([' Bob ', ' about ', ' Zoo ', ' credit '], Cmp_ ignore_case)
"'

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.