匿名函数使用lambda关键字进行定义
定义语法:
lambda 传入参数:函数体(一行代码)
注意事项:
1、lambda匿名函数仅用于构建一个临时函数,只能使用一次,如果要多次使用,需要定义def带名称的函数
2、匿名函数定义中,代码行只能写一行,不允许多行
例:
# 定义一个函数,接受其他函数输入
def test_func(compute):
result = compute(1,2)
print(result)
# 通过lambda匿名函数的形式,将匿名函数作为参数传入
test_func(lambda x,y : x-y)