# 函数作为参数传递
# 把函数传入参数,传递的是代码计算逻辑,不是传入数据
# 定义一个函数,接受另一个函数作为传入参数
def test_func(compute):result = compute(2,3)print(f"compute函数的类型是:{type(compute)}")print(f"函数计算结果是:{result}")
def compute(x,y):return x+y
test_func(compute)
运行结果:

# 匿名函数 lambda# 函数的定义中:
# 1.def关键字,可以定义带有名称的函数
# 2.lambda关键字,可以定义匿名函数(无名称)
# 有名称的函数,可以基于名称重复使用
# 无名称的匿名函数,只可以临时使用一次# 有名称的函数定义,传入函数为参数
def test_func(compute):result = compute(2,3)print(f"compute函数的类型是:{type(compute)}")print(f"函数计算结果是:{result}")
def compute(x,y):return x+y
test_func(compute)# 匿名函数语法: lambda 传入参数 : 函数体(只能写一行代码) 作用更简洁
def test_func(compute):result = compute(2,3)print(f"compute函数的类型是:{type(compute)}")print(f"函数计算结果是:{result}")
test_func(lambda x,y:x+y)
# 两个定义方式功能一样的,区别在于lambda定义函数只能使用一次的临时函数

