[Python] the basis of an anonymous function

First, the definition

  lambda expressions, only one line of code to implement a function;

Second, the syntax specification

  lambda parameter 1, parameter 2, ..: Expression (execution result of the expression is the return value of the function)

  1, no name, lambda expression is equivalent to return an anonymous function (function no name);
  2, the expression can only have one line, return can not appear in this expression, can not appear for..in .., can not appear ... the while
  3, rule parameters and return values of the anonymous function rules and normal function

1  # write function is used to find a number of two and 
2 get_sum = the lambda A, B: A + B
 . 3  Print (get_sum)   # <function <the lambda> AT 0x0000000002206288> 
. 4  Print (get_sum (1, 2))   # . 3 
. 5  
. 6  Print ( the lambda :. 3 <2)   # result of printing <function <the lambda> AT 0x0000000001E76318> 
. 7  
. 8  Print ( the lambda X, Y, Z: X + Y + Z)   # <function <the lambda> AT 0x00000000021E6168>
. 1 the salary = [{ " name " : " Rose " , " the salary " : 1000}, { " name " : " Jack " , " the salary " : 2000 }]
 2  # find the maximum salary with human lambda expression and function max 
. 3 RES1 = max (the salary, Key = the lambda X: x.get ( ' the salary ' ))
 . 4  Print (RES1)   # { 'name': 'Jack', 'the salary': 2000}
1  # by writing a triple expression lambda 
2  DEF FUNC (X, Y):
 . 3      IF X> Y:
 . 4          return X
 . 5      the else :
 . 6          return Y
 . 7  
. 8 RES2 = lambda X, Y: X IF X> Y the else Y
 . 9  Print (RES2 (30, 50))
1  # inner nested function is modified to a lambda expression 
2  DEF func1 ():
 . 3      X = 10
 . 4      # DEF Inner (n-, X): 
. 5      #      return ** n-X 
. 6      # return Inner 
. 7      return  lambda n-, X: ** X n-
 . 8  
. 9  DEF func2 ():
 10      X = 10
 . 11      # DEF Inner (n-, X = X): 
12 is      #      return ** n-X 
13 is      # return Inner 
14      return  the lambda n-, X = X: X ** n
. 1  DEF make_actions ():
 2      acts_list = []
 . 3      for I in Range (. 3):   # I = 0,. 1, 2 
. 4          acts_list.append ( the lambda X: X ** I) # body of the function call when executed 
. 5      return acts_list
 . 6  
. 7  
. 8 acts_list make_actions = ()   # return value acts_list 
. 9  # value i 2 after the end of the cycle, the anonymous function call after loop, the value of i are 2 
10  Print (acts_list [0] (2))   # X the lambda. 4: X ** I (X = 2) ** 2 I -> 2 ** 2 
. 11  Print (acts_list [. 1] (2))   #X the lambda. 4: X ** I (X = 2) ** 2 I -> 2 ** 2 
12 is  Print (acts_list [2] (2))   # . 4 the lambda X: X ** I (X = 2) I 2 ** -> ** 2 2 
13 is  
14  DEF make_actions_two ():
 15      acts_list = []
 16      for I in Range (. 3):   # I = 0,. 1, 2 
. 17          acts_list.append ( the lambda X, I = I : I X **)   # default parameter values, I have a default value, the default value of the parameter assignment timing is defined when the function 
18 is      return acts_list
 . 19  
20 is  
21 is acts_list make_actions_two = ()   # return value acts_list 
22 is  Print(acts_list[0](2))  # 0  lambda x: i ** x (x=2)  i**2 --> 0**2
23 print(acts_list[1](2))  # 1  lambda x: i ** x (x=2)  i**2 --> 1**2
24 print(acts_list[2](2))  # 4  lambda x: i ** x (x=2)  i**2 --> 2**2

Guess you like

Origin www.cnblogs.com/Tree0108/p/12110160.html