Understanding lambda expressions in Python

Understanding lambda expressions in Python

A lambda expression can be thought of as a kind of anonymous function. It allows you to quickly define an extremely simple one-liner function .

add3 = lambda a, b, c: a + b + c
print(add3(1,3,4))

The result is: 8

Syntax format of lambda expression:

lambda parameter list: expression

When defining a lambda expression, there are no parentheses around the parameter list , no return keyword before the return value , and no explicitly defined function name.

The writing method is more concise than def. However, its body can only be an expression , not a code block , or a command (such as del). Therefore, lambda expressions lose certain functions while gaining brevity, and the logic that can be expressed is relatively limited. A lambda expression creates a function object that can be called by assigning it to a variable.

Guess you like

Origin blog.csdn.net/qq_39065491/article/details/131131531