Callback in Python

Source: Internet
Author: User

    • 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.