跳转至

回调函数

回调的概念 #card

回调是回调函数(callback function)的简称,回调函数就是一个被作为参数传递的函数。 ^1662485202460

为什么要用回调 #card

回调函数通常在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。 ^1662485202471

回调函数案例 #card

def callbackFunc1(s):
    print('Callback Function 1: Length of the text file is : ', s)
def callbackFunc2(s):
    print('Callback Function 2: Length of the text file is : ', s)
def printFileLength(path, callback):
    f = open(path, "r")
    length = len(f.read())
    f.close()
    callback(length)
if __name__ == '__main__':
    printFileLength("sample.txt", callbackFunc1)
    printFileLength("sample.txt", callbackFunc2)
^1662485202479

参考文献

  1. Python Callback Function - Python Examples

最后更新: 2022年10月15日 01:02:48
创建日期: 2022年7月24日 06:02:09
Contributers: yangjh