Lambda, an anonymous function of Python

Source: Internet
Author: User

Today, when I look at the example of Yate embedded python, I forgot what Lambda is, too faint! The tag is as follows.

Example:

def route(yate):    def on_route(route):        ...    yate.onmsg("call.route",        lambda m : m["called"] == "ivr").addCallback(on_route)

Use Lambda in Python to create anonymous functions. Lambda comes from the lisp language. The Lambda format is as follows:

  • Lambda arg1, arg2...:<Expression>

Lambda creates a function object, but does not assign this function object to an identifier. Def is different. It performs this operation when creating a function object. This is the first feature of Lambda. Lambda's second feature is that it is just an expression, not a statement. If you make it a separate statement, for example:

  • Lambda X: Print x

If you write such a line in your python program, it is meaningless. This line of code creates a function object, but it is discarded immediately, because you didn't use its return value, that is, the function object. Lambda is just an expression that can be directly used as a list or dictionary Member, for example:

  • L = [Lamba X: X ** 2, Lambda X: X ** 3]

There is no way to directly replace it with the def statement. Third, a Lambda expression can have only one expression after. That is to say, in def, it can be placed after return or lambda, not after return or here. More specifically, the following expression can return a value. If the value cannot be returned, it cannot be placed here. Therefore, statements such as if, for, or print cannot be used in Lambda. Lambda is generally only used to define simple functions. Of course, some tips can be used to implement the same functions as if or for in Lambda. For example, if statements can be simulated using the "Short Circuit" feature of the logical operators "and" or ", for example:

  • (Test and [X]) or [y]) [0]

In this case, if test is true, [x] is calculated. Of course, [x] is obtained, because the true value is obtained on the left of the OR operator, the right side of or is not calculated. Therefore, [x] [0] is obtained, and the final result is X. If test is false, the left is false Based on the and feature, and the right is not calculated. If the left side of or is false, [y] [0] is obtained. The final result is Y. Note that the following format cannot be used:

  • (Test and X) or Y

When X is the true value, this form is equivalent to the above form. However, imagine this situation: "If test is true, take 0. If test is false, take []. That is to say, X itself is a dummy value. In the above form, it is written as follows:

  • (Test and 0) or []

Obviously it cannot achieve the goal. This formula will always only get []. Therefore, it should be rewritten:

  • (Test and [0]) or [[]) [0]

In lambda, loop statements can also be simulated, and map functions are used. For example:

  • F = Lambda X: Map (lambda Y: Y ** 2), X)

Of course, this kind of thing looks complicated. If possible, it is better not to use Lambda nested. Print can also be simulated:

  1. Import sys
  2. Pp = Lambda X: SYS. stdout. Write (STR (x) + '/N ')
  3. PP (8) ==> 8

From above: http://hi.baidu.com/peaksst/blog/item/bba63eaf694cdaf9fbed508e.html

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.