python 高階函數與裝飾器

來源:互聯網
上載者:User

標籤:原則   調用   嵌套   密碼   其他   time   login   pytho   opp   

 

 

高階函數定義
1.函數接收的參數是一個函數名
2.函數的傳回值是一個函數名
以上兩者滿足任意一個,就是高階函數

裝飾器定義
本質就是函數,功能是為其他函數添加新功能
裝飾器的原則

   1.不修改被裝飾函數的原始碼(開放封閉原則)

   2.為被裝飾函數添加新功能後,不修改被修飾函數的調用方式

 

   裝飾器=高階函數+函數嵌套+閉包

   

# 無傳回值無參數import timedef timer(func):     #func = test    def w():        start_time = time.time()        func()     #就是在運行test()        stop_time = time.time()        print("已耗用時間是%d"%(stop_time-start_time))    return w@timer    # 相當於 test = timer(test)def test():    time.sleep(2)    print("from test")# test = timer(test)  #返回的是w的地址# test()       #相當於執行wtest()

 

#加上傳回值import timedef timer(func):       #func = test    def w():        start_time = time.time()        res = func()    #就是在運行test()        stop_time = time.time()        print("已耗用時間是%d"%(stop_time-start_time))        return res    return w@timer    # 相當於 test = timer(test)def test():    time.sleep(2)    print("from test")    return "這是test的傳回值"# test = timer(test)  #返回的是w的地址# test()       #相當於執行wres = test()print(res)

 

#加上參數和傳回值 裝飾器最終形式import timedef timer(func):          #func = test   #func = test1    def w(*args,**kwargs):        start_time = time.time()        res = func(*args,**kwargs)     #就是在運行test()   test1()        stop_time = time.time()        print("已耗用時間是%d"%(stop_time-start_time))        return res    return w@timer    # 相當於 test = timer(test)def test(name,age):    time.sleep(2)    print("from test   %s   %s"%(name,age))    return "這是test的傳回值"res = test("liao",18)print(res)@timer     # 相當於 test1 = timer(test1)def test1(name,age,g):    time.sleep(1)    print("from test1   %s   %s  %s"%(name,age,g))    return "這是test1的傳回值"res1 = test1("bo",26,"shi")print(res1)

 

使用者登陸(簡單流程判斷)

l = [{"name":"liao","pwd":"123"},{"name":"tom","pwd":"123"}]     #使用者資料c_d = {"user":None,"login":False}          #定義一個空的臨時的使用者字字典def a_f(func):    def w(*args,**kwargs):        if c_d["user"] and c_d["login"]:      #判斷臨時字典裡是否有使用者登陸,沒有就輸入            res = func(*args,**kwargs)        #有就進入下一步            return res        user = input("請輸入使用者名稱:").strip()   #臨時字典沒有資料就輸入使用者名稱        pwd = input("請輸入密碼:").strip()      #臨時字典沒有資料就輸入密碼        for i in l:          #遍曆使用者資料            if user == i["name"] and pwd == i["pwd"]:    #判斷輸入的使用者和密碼是否在使用者資料裡                c_d["user"] = user        #輸入正確,資料儲存到臨時的使用者字字典裡,下一步不用再輸入使用者和密碼                c_d["login"] = True                res = func(*args,**kwargs)    #進入                return res        else:                       #如果輸入的使用者名稱和密碼不在用記資料裡,提示使用者            print("使用者名稱或者密碼錯誤")    return w@a_fdef index():    print("歡迎來到首頁面")@a_fdef home():    print("這裡是你家")@a_fdef shopping_car():    print("查看購物車啊親")index()home()shopping_car()

 

 

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.