Python higher order function: Partial (partial function)

Source: Internet
Author: User

Format:

Functools.partail (function, function parameter)-------> int (x, base)-----> functools.partial (int, base)


One, int function

Website Introduction:classint(x, base=10#x为字符串数字, the default string number is a decimal number and the return value is a decimal number



#!/usr/bin/python

# #普通使用
print "int (' 10001 ', 2):", int (' 10001 ', 2) #字符串数字 ' 10001 ' is a 2 binary, the return value is a decimal number


# #自定义使用

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

Print "Int2 (' 1010101 '):", Int2 (' 1010101 ')


Second, partial function

Website Introduction:

  • functools.partial(func[,*args][, **keywords])?

  • Return a newpartialObject which when called would behave likefuncCalled with the positional argumentsargsand keyword argumentskeywords. Ifmore arguments is supplied to the call, they is appended toargs. ifadditional keyword arguments is supplied, they extend and overridekeywords.


Import Functools #导入模块

Int3 = functools.partial (int, base=2) # Int3: Sets the base parameter of the INT function to the default value 2 binary
Print "Int3 ('):", Int3 (' 100 ')


INT4 = functools.partial (int, ' 2 ') #int4: sets the x parameter of the INT function to the default value ' 2 ', and its base default is decimal

Print "Int4 (' 2 '):", Int4 ()

Python higher order function: Partial (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.