Anonymous functions, adorners, and partial functions of python functional programming

Source: Internet
Author: User
anonymous functions

When we pass in a function, there are times when we do not need to explicitly define the function and pass in the anonymous function directly. As below

lambda x: x*x

In Python, the keyword lambda represents an anonymous function, and the x preceding the colon represents the function parameter
The anonymous function has a restriction that there can be only one expression, without writing return, the return value is the result of that expression.
Benefits: Prevent function name collisions, in addition to anonymous functions is a function object, you can also assign an anonymous function to a variable, and then use the variable to invoke the function.

Adorner (decorator)

Decorator can enhance the function of functions, although it is a bit complicated to define, but it is very convenient to use.
The specific reference Liaoche is the official tutorial. Https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 0014318435599930270c0381a3b44db991cd6d858064ac0000

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

Python's 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 that does not require us to define INT2 ().

import functoolsint2 = functools.partial(int,base = 2)int2(‘1000000‘)//64

Anonymous functions, adorners, and partial functions of python functional programming

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.