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
原文地址:https://www.cnblogs.com/xhcdream/p/8159336.html