Python Learning note __4.5 partial function (binding function parameter)

Source: Internet
Author: User
Tags pow

# This is a learning note for the Liaoche teacher Python tutorial

1. Overview

Partial function is a function of the Functools module

The role of Functools.partial is based on an existing function. It is easier to call this new function by pinning some of its parameters (that is, setting the default value) and then returning a new function .

1.1, Functools. Partial application

Known:

The INT function can be ' 123 '--123.

The base parameter of int can be converted in N-binary

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

Use:

# Partial defines a function that has a fixed 2-binary conversion

Int2 = functools.partial (int, base=2) # After the definition, we can still replace the base value with other values, just like the default parameters

Analytical:

When you create a partial function, you can actually receive the 3 parameters of the function object, *args, and **kw.

When incoming:

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

Equivalent:

KW = {' base ': 2} # Fixed a keyword argument

Int (' 10010 ', **kw)

When incoming:

Max2 = functools.partial (max) # 10 is automatically added to the right as part of the *args

Equivalent:

args = (10, 5, 6, 7)

Max (*args)

Add:

For cases like the POW () parameter, the range () parameter, has multiple parameters.

P=functools.partial (pow,2), the first parameter is fixed


Python Learning note __4.5 partial function (binding function parameter)

Related Article

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.