函数式编程

闭包,有点类似

例子:有自己内部变量的函数

def transaction():
  sqls = []
  def insert(sql):
    sqls.append(sql)
  def update(sql):     sqls.append(sql)
  def commit():     conn = self.__getConn()     cur = conn.cur()     for s in sqls:       cur.execute(s)     cur.commit()     sqls = []   return {'insert':insert, 'update':update, 'commit':commit}

  

例子:延迟调用

def DelayCall(func, **argc):
  def ret():
    func(**argc)
  return ret

例子:MapReduce

a = {'x':1, 'y':''}
# 检测是否每个字段都为真
reduce(lambda x, y:x and bool(y[1]), a.items(), True)

  

posted on 2017-12-07 20:27  willaty  阅读(136)  评论(0编辑  收藏  举报

导航