# -*-coding:utf-8-*- # Anonymous Functions # anonymous function syntax format " " variable = lambda parameter list: expression " " Lambda x,y:x+= func (2,3)print(a)# an anonymous function is similar to a macro definition in C, and can only perform some simple calculations # Unlike normal functions, anonymous functions can return operation results even if there is no return keyword
#-*-coding:utf-8-*-#The application of anonymous function in the sort () function" "python is usually a line to write all the code, if you encounter a line to write the case, there are two ways: 1. At the end of the line of code, add the continuation line "\" (that is, the space +\); test = ' item_one ' item_two ' Tem_three ' Output: ' Item_oneitem_twotem_three ' 2. Parentheses, no special line breaks are required in () {} []: Test2 = (' csdn ' cssdn ') output: csdn cssdn" "Stu= [{"name":"Tom"," Age": 15},{"name":"Jack"," Age": 13},{"name":"Li"," Age": 12}]#The x here is every element in the list.#key = lambda ... This is a fixed notation, the variable name must use the key, the other variable name does not work#The key here is essentially specifying the parameter name to pass the argumentStu.sort (aa =Lambdax:x["name"])Print(Stu)
# -*-coding:utf-8-*- # function pointers such as anonymous functions assign values def Compute (A, B, func): return func (A, b) Print Lambda x,y:x+"Lambda x,y:x*y"#eval () function can convert a string into an expression Pcmd = eval (strcmd)printLambda x,y:x*y))
Python Anonymous Parameters