Python Functional Programming--anonymous function lambda

Source: Internet
Author: User

1.1   Anonymous Functions

Sometimes it is easier to pass in anonymous functions without explicitly defining a function.

>>> list (map (Lambda x:x*x, (1, 2, 3, 4, 5))

[1, 4, 9, 16, 25]

before the colon x represents a function parameter .

there is a limit to the anonymous function, that is, there can be only one expression , without writing return, the return value is the result of that expression.

>>> f = Lambda x:x * x

>>> F (2)

4

>>> def build (x, y):

... return x * x + y * y

...

>>> Build (2, 4)

20

>>> def build (x, y):

... return lambda:x * x + y * y

...

>>>

>>> Build (2, 4)

<functionbuild.<locals>.<lambda> at 0x2b8091d87598>

>>> Build (2,4) ()-- call -->lambda again

20


This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1820951

Python Functional Programming--anonymous function lambda

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.