匿名函数

list = [{"name":"hello","age":12},{"name":"world","age":5},{"name":"today","age":23}]
list.sort(key=lambda x:x["age"])
print(list)

匿名函数当做实参

def test(a,b,func):
    result = func(a,b)
    return result

num = test(11,22,lambda x,y:x+y)
print(num)

匿名函数的应用

def test(a,b,func):
    result = func(a,b)
    return result

func_new = input("请输入一个匿名函数:")
func_new = eval(func_new)
num = test(11,22,func_new)
print(num)

 

posted @ 2020-03-14 23:26  红色天空下  阅读(78)  评论(0编辑  收藏  举报