Python中的lambda函数

今天在看书的时候,看到了这样的一条语句:

if isinstance(value,int) or isinstance(value,float):
    split_function=lambda row:row[column]>=value 

对其中的lambda这个函数表示很不明白,于是看了看Python文档,文档中解释如下:

  lambda
An anonymous inline function consisting of a single expression which is evaluated when the function is called. The syntax to create a lambda function is lambda [arguments]: expression
lambda函数也叫匿名函数,即,函数没有具体的名称,而用def创建的方法是有名称的。
lambda允许用户快速定义单行函数,当然用户也可以按照典型的函数定义完成函数。lambda的目的就是简化用户定义使用函数的过程。
例如:
log2=lambda x:log(x)/log(2)

就是定义了一个以2为底的对数函数。这个例子是有参数的,还有一种情况是没有参数的:

>>>bar=lambda :'this is a bar'
>>>print bar
this is a bar

 

posted on 2015-12-02 20:08  波比12  阅读(20797)  评论(0编辑  收藏  举报

导航