python回呼函數

來源:互聯網
上載者:User

標籤:地址   ret   說明   hang   另一個   api   抽油煙機   []   gpo   

1、什麼是回呼函數,感覺好複雜??存在的意義是什嗎?

回呼函數:通過函數指標調用的函數。如果你把函數的指標(地址)作為參數傳遞給另一個函數,當這個指標被用來調用其所指向的函數時,我們就說這是回呼函數。

如下面的例子1:來源  https://www.cnblogs.com/hainan-zhang/p/6222552.html

def clean1(times):    """    就假裝掃地    param times :次數    return None    """    print(‘已完成掃地次數:‘,times)def clean2(times):    """    假裝洗抽油煙機    param times:次數    return: None    """    print(‘已洗抽油煙機次數:‘,times)def call_clean(times,function_name):    """    這個很重要,就是家政公司的業務系統,要啥業務都得在這說    這個是實現回呼函數的核心    param times:次數    param function_name:回呼函數名    return:調用的函數結果    """    return function_name(times)call_clean(10,clean2)   #call_clean(10,clean2)

這個例子介紹回呼函數的使用,沒有說明回呼函數存在的意義?這樣寫還不如直接調用clean2函數10次。

下面在網上找的另外一個,例子2:http://blog.csdn.net/ma52103231/article/details/61432120

看著有點懵,難度大,換一個

class Callback:    def __init__(self,instance,function_name):        self.instance=instance        self.function_name=function_name    def action(self,params):        self.instance.__getattribute__(self.function_name)(params)class Test:    def __init__(self):        self.clb=None    def register(self,clb):        self.clb=clb    def do_test(self):        params=[]        self.clb.action(params)class Api:    def __init__(self,test_instance):        test_instance.register(Callback(self,self.function.__name__))    def function(self,params):        print(‘function‘)t=Test()a=Api(t)t.do_test()#function

例子3:http://blog.csdn.net/ma52103231/article/details/61432120

 

python回呼函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.