您的位置:首页 > 其它

今日收获:匿名函数 Lambda

2017-10-18 22:27 148 查看
 # Anonymous Fuction Lambda

 - Lambda is an anonymous function, which saves our time in process of defining one.

 - Arguments and ouputs are seperated by a colon
 - The latter can be simply a function or conditional statments or other complex expressions.

Examples

 - Single argument

f1 = lambda x: 1/x
>>> f1(2)
0.5

 - Multi argument
f2 = lambda x,y : x+y
>>> f2(2, 3)
5

 - Conditional expression
f3 = lambda x,y: 'fucked up' if (x%y != 0) else 'in progress'
>>> f3(1, 2)
'fucked up'
>>>f3(4, 2)
'in progress'

语法风格与C很相似呢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lambda 函数