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:
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:
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:
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:
- Import sys
- Pp = Lambda X: SYS. stdout. Write (STR (x) + '/N ')
- PP (8) ==> 8
From above: http://hi.baidu.com/peaksst/blog/item/bba63eaf694cdaf9fbed508e.html