Python---anonymous function lambda

Anonymous function----lambda (cannot be reused)
Anonymous function definition syntax:
lambda Incoming parameters: function body (one line of code)
lambda is a keyword, indicating the definition of an anonymous function.
The incoming parameters represent the formal parameters of the anonymous function, such as: xy Indicates that the function body receives two formal parameters, which is the execution logic of the function. Please note: you can only write one line, and you cannot write multiple lines of code.

# 有名称函数 ----- def
# 匿名函数 ---- lambda
# 匿名函数定义语法:
# lambda 传入参数: 函数体(一行代码)
# lambda 是关键字,表示定义匿名函数
# 传入参数表示匿名函数的形式参数,如:xy表示接收2个形式参数函数体,就是函数的执行逻辑,要注意:只能写一行,无法写多行代码
def user_info5(compute1):
    result = compute1(2, 2)
    print(result, type(compute1))  # 3 <class 'function'>


# 有名称函数
def compute1(x, y):
    return x + y
user_info5(compute1)


# 匿名函数
user_info5(lambda x, y: x + y)

Guess you like

Origin blog.csdn.net/weixin_52053631/article/details/132867085