• python 回调函数


    def apply_async(func, args,  callback):
    # Compute the result
       result = func(*args)
    # Invoke the callback with the result
       callback(result)
    def print_result(result):
       print('Got:', result)
    
    def add(x, y):
       return x + y
    apply_async(add, (2, 3), callback=print_result)
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/cookbook/a22.py
    ('Got:', 5)
    
    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    def apply_async(func, args,  callback):
    # Compute the result
       result = func(*args)
    # Invoke the callback with the result
       callback(result)
    def print_result(result):
       print('Got:', result)
    
    def add(x, y):
       return x + y
    apply_async(add, (2, 3), callback=print_result)
    class ResultHandler:
       def __init__(self):
          self.sequence = 0
       def handler(self, result):
          self.sequence += 1
          print ('[{}] Got: {}'.format(self.sequence, result))
    
    ##实例化对象
    r = ResultHandler()
    ##r.handler 调用对象方法
    apply_async(add, (2, 3), r.handler)
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/cookbook/a22.py
    ('Got:', 5)
    [1] Got: 5
    
    

  • 相关阅读:
    css 字体相关属性的设置
    flex 三列布局
    python shelve模块
    python collections模块
    Python shutil模块,高级文件管理
    python中os模块的常用
    python中hashlib加密模块和sys系统模块
    logging日志模块
    random模块常用方法
    序列化json和pickle模块
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349438.html
Copyright © 2020-2023  润新知