python回调函数的使用方法

Python015

python回调函数的使用方法,第1张

python回调函数的使用方法

在计算机程序设计中,回调函数,或简称回调(Callback),是指通过函数参数传递到其它代码的,某一块可执行代码的引用。这一设计允许了底层代码调用在高层定义的子程序

有两种类型的回调函数:

那么,在python中如何实现回调函数呢,看代码:

代码如下:

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)

在计算机程序设计中,回调函数,或简称回调(Callback),是指通过函数参数传递到其它代码的,某一块可执行代码的引用。这一设计允许了底层代码调用在高层定义的子程序:

例如:

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)

执行结果是:

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