Python中的‘lambda’关键字提供了一个便捷的途径去声明短小并匿名的函数。
1# The lambda keyword in Python provides a 2# shortcut for declaring small and 3# anonymous functions: 4 5>>> add = lambda x, y: x + y 6>>> add(5, 3) 78 8 9# You could declare the same add() 10# function with the def keyword: 11 12>>> def add(x, y): 13... return x + y 14>>> add(5, 3) 158 16 17# So what's the big fuss about? 18# Lambdas are *function expressions*: 19>>> (lambda x, y: x + y)(5, 3) 208 21 22# • Lambda functions are single-expression 23# functions that are not necessarily bound 24# to a name (they can be anonymous). 25 26# • Lambda functions can't use regular 27# Python statements and always include an 28# implicit `return` statement.
Lambda函数是单一表达式函数,即无需绑定名称。
Lambda函数不能使用常规的语句,通常包含一个隐式的返回语句。