匿名函数:lambda
f = lambda x,y,z: x+y+z f(2,3,4) >>9
def knights(): title = 'Sir' action = (lambda x:title+''+x) return action act = knights() act('robin') >>'Sir robin'
在序列中映射函数:map
counters = [1,2,3,4] def inc(x):return x+10 list(map(inc, counters)) >>[11,12,13,14]
函数式编程工具:filter和reduce
list(filter((lambda x:x>0), ranger(-5,5))) >>[1,2,3,4]
from functools import reduce reduce((lambda x,y:x+y), [1,2,3,4]) >>10