标签:fp
回调
函数作为参数
函数执行=> 到一个参数函数时=> 调用另一个函数=> 回到主函数
#!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = ‘teng‘ def test(callback): print ‘test func begin‘ callback() def test1(callback): print ‘test1 func begin‘ for func in callback: func() def cb1(): print ‘callback 1‘ def cb2(): print ‘callback 2‘ if __name__==‘__main__‘: test(cb1) test1((cb1, cb2))
以下是一个回调和项目中的应用
#!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = ‘teng‘ def send_weixin(addr, message): print u"发微信("+message+u")给"+addr def send_email(addr, message): print u"发邮件("+message+u")给"+addr def send_qq(addr,message): print u"发QQ("+message+u")给"+addr def send_duanxin(addr, message): print u"发短信("+message +u")给"+addr send_method ={ ‘QQ‘:send_qq, ‘WeiXin‘:send_weixin, ‘DuanXin‘:send_duanxin, ‘Email‘:send_email } def sendgupiao(message, custom_file, vip=False): # 处理message # 判断是否Vip # 解析文件,得到地址 # f(message, addr) send_s =[] for s in message: if vip == False: t = s.split(‘,‘) send_s.append(t[0]) else: send_s.append(s) send_message = ‘;‘.join(send_s) for line in open(custom_file, ‘r‘): info = line.strip().split(‘,‘) send_method[info[0]](info[1], send_message) #info[0]为调用方式 if __name__==‘__main__‘: sendgupiao([u"000001买,低于8.5买",u"000002卖,高于11.2卖"],‘custom_info.txt‘) print "################### VIP ####################" sendgupiao([u"000001买,低于8.5买",u"000002卖,高于11.2卖"],‘custom_info.txt‘,vip=True)
标签:fp
原文地址:http://tengrommel.blog.51cto.com/608570/1741342