zoukankan      html  css  js  c++  java
  • perl 和 python中的回调函数

    就是向函数的参数为   一个函数的引用
    sub  callback1 {
        ......
    }
    
    sub  callback2 {
       ......
    }
    
    sub run {
       my $func_ref = shift;
      .....
      ....
    }
    if (a>b) {
         run(&callback1);
    }
    else {
        run(&callback2);
    }
    
    
    def my_callback(input):
        print "function my_callback was called with %s input" % (input,)
    
    
    def caller(input, func):
        func(input)
    
    
    for i in range(5):
        caller(i, my_callback)
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/cookbook/a3.py
    function my_callback was called with 0 input
    function my_callback was called with 1 input
    function my_callback was called with 2 input
    function my_callback was called with 3 input
    function my_callback was called with 4 input
    
    
    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*2
    print  apply_async(add, (2, 3), print_result)
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/cookbook/a4.py
    ('Got:', 8)
    None

  • 相关阅读:
    什么是IOC
    spring的作用
    什么是spring框架?
    72
    71
    70
    69
    68
    67
    66
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349460.html
Copyright © 2011-2022 走看看