-
Callback
function as a parameter
function execution = to a parameter function = = Call another function = = return to the main function
-
#!/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))
The following is a callback and an application in the project
#!/usr/bin/env python# -*- coding:utf-8 -*-__author__ = ' Teng ' def send_ Weixin (addr, message): print u "Hair (" +message+u ") to" +addrdef send_email (addr , message): print u "Send mail (" +message+u ") to" +addrdef send_qq (addr,message): print u "Send QQ (" +message+u ") to" +addrdef send_duanxin (addr, message): print u "Send SMS (" +message +u ") to" +addrsend_method ={ ' QQ ": send_qq, ' Weixin ':send_weixin, ' DuanXin ':send_duanxin, ' Email ': Send_email}def sendgupiao (message, custom_file, vip=false):# Handle message# determine if vip# parse file, get address # f (MESSAGE, ADDR) send_s =[] for s in message: if vip == false:&nbsP; 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] for Call mode if __name__== ' __main__ ': sendgupiao ([u] 000001 buy, less than 8.5 buy ", U" 000002 sell, above 11.2 sell "], ' custom_info.txt ') print "################### vip ####################" sendgupiao ([u] 000001 buy, less than 8.5 buy ", U" 000002 sell, higher than 11.2 sell "], ' custom_info.txt ', vip=true)
Callback in Python