lambda应用

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

test(10, 15, lambda x, y: x + y)

 

#coding=utf-8
#python2需要加上coding=utf-8

def test(a, b, func):
    result = func(a, b)
    return result
#python2中的方式
#func_new = input("请输入一个匿名函数:")

#python3中的方式
func_new = input("请输入一个匿名函数:")
func_new = eval(func_new)

num = test(11, 22, func_new)
print(num)

结果:
请输入一个匿名函数:lambda x, y: x - y
-11

 

posted @ 2017-12-31 23:02  许小伍  阅读(119)  评论(0编辑  收藏  举报