函数的定义方式可以分为两种,def关键字,可以定义带有名称的函数;lambda关键字,可以定义匿名函数(无名称)
- 有名称的函数,可以基于名称重复使用
- 无名称的匿名函数,只可临时使用一次
匿名函数定义语法:
Lambda 传入参数:函数体
函数可作为参数传递
def count(a, b):
return a + b
def test(fun):
print(fun(1, 2))
test(count)
替换为匿名函数
def test(fun):
print(fun(1, 2))
test(lambda x, y: x + y)