2016年5月30日 星期一

Python_Lambda


lambda 為 Python 的關鍵字 (keyword) 之一,用來建立無名函數 (anonymous function)

https://docs.python.org/3/reference/expressions.html#lambda
lambda_expr        ::=  "lambda" [parameter_list]: expression
lambda_expr_nocond ::=  "lambda" [parameter_list]: expression_nocond
Lambda expressions (sometimes called lambda forms) are used to create anonymous functions. The expression lambda arguments:expression yields a function object. The unnamed object behaves like a function object defined with
def <lambda>(arguments):
    return expression
See section Function definitions for the syntax of parameter lists. Note that functions created with lambda expressions cannot contain statements or annotations.

範例:
f = lambda x: x ** x
雷同
def f(x):
return x**x

多變數用法
g = lambda x, y: x * y


沒有留言:

張貼留言