好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > python基礎之裝飾器詳解

python基礎之裝飾器詳解

熱門標簽:地圖標注微信發送位置不顯示 地圖制圖標注位置改變是移位嗎 南京銷售外呼系統軟件 房產電銷外呼系統 315電話機器人廣告 蓋州市地圖標注 上海機器人外呼系統哪家好 浙江電銷卡外呼系統好用嗎 地圖標注的意義點

一、前言

裝飾器:本質就是函數,功能是為其他函數添加附加功能

原則:

  •     1、不修改被修飾函數的源代碼
  •     2、不修改被修飾函數的調用方式

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

二、高階函數

高階函數定義:

  •     1、函數接收的參數是一個函數
  •     2、函數的返回值是一個函數名
  •     3、滿足上述條件任意一個,都可以稱為高階函數

test 函數是高階函數,接受了一個foo 作為參數

import time
def foo():
    time.sleep(3)
    print("sleep 3s")
 
def test(func):
    start_time = time.time()
    func()
    stop_time = time.time()
    print("函數的運行時間是: %s" % (stop_time - start_time))
 
test(foo)

timer 是一個高階函數,這個函數返回值是一個函數

import time
def foo():
    time.sleep(3)
    print("sleep 3s")
 
def timer(func):
    start_time = time.time()
    func()
    stop_time = time.time()
    print("執行時間{}".format(stop_time - start_time))
    return func
foo = timer(foo)
foo()
# 結果: 多運行了一次

三、函數嵌套

在函數里面定義函數,變量的作用域和生存周期不變。

def father(name):
    print("father name: %s" % name)
    def son():
        print("son name: %s" % name)
    son()
father("xu1")
 
# 結果:
#     father name: xu1
#     son name: xu1

四、裝飾器

實現一個計算函數執行時間的函數作為裝飾器,用來計算被裝飾函數的執行時間并打印

import time
 
def timer(func):  # 實現一個計算函數執行時間的函數作為裝飾器,用來計算被裝飾函數的執行時間并打出
    def wrapper():
        start_time = time.time()
        func()
        stop_time = time.time()
        print("運行時間: %s" % (stop_time - start_time))
    return wrapper
 
# def test():  # 不使用裝飾器的同等實現
#     time.sleep(3)
#     print("test sleep 3s")
#
# test = timer(test)  # 返回的是 wrapper 的地址
# test()  # 執行的是 wrapper
 
 
@timer
def test():  # 裝飾器的實現
    time.sleep(3)
    print("test sleep 3s")
 
test()  # 執行的是 wrapper
# 結果:
#     test sleep 3s
#     運行時間: 3.000915050506592

4.1 被裝飾方法帶返回值

import time
 
 
def timer(func):
    def wrapper():
        start_time = time.time()
        res = func()  # 執行被裝飾方法
        stop_time = time.time()
        print("運行時間: %s" % (stop_time - start_time))
        return res  # 接受正在調用的方法的返回值,并返回
    return wrapper
 
 
@timer
def test():
    time.sleep(3)
    print("test sleep 3s")
    return "test return ok"
 
 
print(test())  # 執行的是 wrapper
# 結果:
#     test sleep 3s
#     運行時間: 3.0002923011779785
#     test return ok

4.2 被裝飾方法帶參數

import time
 
 
def timer(func):
    """
        *args:將被修飾方法傳入的非關鍵字參數打包為元組 args
        **kwargs: 將被修飾方法傳入的關鍵字參數打包為字典 kwargs
    """
    def wrapper(*args, **kwargs):
        start_time = time.time()
        res = func(*args, **kwargs)  # *args 拆解元組,按順序傳給被修飾函數; **kwargs:拆解字典
        stop_time = time.time()
        print("運行時間: %s" % (stop_time - start_time))
        return res
    return wrapper
 
 
@timer  # 給test 方法添加計算執行時間的裝飾器
def test(name, age):
    time.sleep(3)
    print("name = {}, age = {}".format(name, age))
    return "test return ok"
 
 
# 調用被裝飾器裝飾的方法
print(test("xu", 100))  # 執行的是 wrapper
# 結果:
#     name = xu, age = 100
#     運行時間: 3.000420331954956
#     test return ok

4.3 驗證功能裝飾器

假如 index() 、home()、shopping_car() 三個方法都需要登錄后才能訪問(無法訪問時里面不輸入對應內容),正常情況下只需登錄一次,后面訪問其他方法就無需再次登錄。

可以通過@auth_fun裝飾器進行驗證用戶是否登錄,如果沒有就讓用戶輸入賬號密碼,用戶賬號密碼正確的記錄當前登錄的用戶,其他方法無需再次登錄。

# 用戶列表
user_list = [
    {'name': 'xu1', 'passwd': '123'},
    {'name': 'xu2', 'passwd': '123'},
    {'name': 'xu3', 'passwd': '123'},
    {'name': 'xu4', 'passwd': '123'},
]
# 當前登錄的用戶
current_dic = {"username": None, "login": False}
 
 
# 驗證用戶是否登錄的裝飾器
#   如果用戶沒有登錄,讓用戶輸入賬號密碼,校驗通過記錄用戶狀態
def auth_fun(func):
    def wrapper(*args, **kwargs):
        if current_dic["username"] and current_dic['login']:
            res = func(*args, **kwargs)
            return res
        username = input("請輸入用戶名:")
        pw = input("請輸入密碼:")
        for u in user_list:
            if u["name"] == username and u["passwd"] == pw:
                current_dic["username"] = username
                current_dic["login"] = True
                res = func(*args, **kwargs)
                return res
        else:
            print("用戶沒有注冊!")
    return wrapper
 
 
@auth_fun
def index():
    print("this is index")
 
 
@auth_fun
def home():
    print("this is home page")
 
 
@auth_fun
def shopping_car():
    print("this is shopping car")
 
 
index()  # 輸入用戶密碼
home()  # index 已經登錄,無需在輸入
shopping_car()  # index 已經登錄,無需在輸入
# 結果:
#     請輸入用戶名:xu1
#     請輸入密碼:123
#     this is index
#     this is home page
#     this is shopping car

4.4 驗證功能裝飾器——帶參數

 裝飾器帶參數,最簡單的操作就是可以對被裝飾的函數進行區別處理。

# 用戶列表
user_list = [
    {'name': 'xu1', 'passwd': '123'},
    {'name': 'xu2', 'passwd': '123'},
    {'name': 'xu3', 'passwd': '123'},
    {'name': 'xu4', 'passwd': '123'},
]
# 當前登錄的用戶
current_dic = {"username": None, "login": False}
 
"""
    注意:帶參數的裝飾器會比沒有帶參數的裝飾器多嵌套一層函數(多了auth)
        調用方式是 @auth(auth_type="type1"), 返回 auth_fun,
        也就是說 @auth(auth_type="type1")相當于 @auth_fun
        但是 auth_fun 函數所在的嵌套作用域多了一個 auth_type 的變量
"""
def auth(auth_type="type1"):
    def auth_fun(func):
        def wrapper(*args, **kwargs):
            if auth_type == "type1":
                if current_dic["username"] and current_dic['login']:
                    res = func(*args, **kwargs)
                    return res
                username = input("請輸入用戶名:")
                pw = input("請輸入密碼:")
                for u in user_list:
                    if u["name"] == username and u["passwd"] == pw:
                        current_dic["username"] = username
                        current_dic["login"] = True
                        res = func(*args, **kwargs)
                        return res
                else:
                    print("用戶沒有注冊!")
            elif auth_type == "type2":
                print("不用授權直接登錄: type = {}".format(auth_type))
                res = func(*args, **kwargs)
                return res
            else:
                print("其他type沒有實現")
        return wrapper
    return auth_fun
 
 
"""
    auth_fun = @auth(auth_type="type1") 
    auth_fun 所在的嵌套與將有一個 auth_type 變量
    然后通過 @auth()方法返回的對象注解 index,相當于 @auth_fun 注解index 方法,最后得到 wrapper 對象
"""
@auth(auth_type="type1")
def index():
    print("this is index")
 
 
@auth(auth_type="type2")
def home():
    print("this is home page")
 
 
@auth(auth_type="type3")
def shopping_car():
    print("this is shopping car")
 
 
home()  # 注意:auth_type="type2",這個方法無需登錄可以直接執行
index()  # 注意:auth_type="type1",需要登錄
shopping_car()  # 注意:auth_type="type3",沒有做處理
# 結果:
#     不用授權直接登錄: type = type2
#     this is home page
#     請輸入用戶名:xu1
#     請輸入密碼:123
#     this is index
#     其他type沒有實現

到此這篇關于python基礎之裝飾器詳解的文章就介紹到這了,更多相關python裝飾器內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 詳解Python裝飾器之@property
  • python 裝飾器的使用與要點
  • python高級語法之閉包和裝飾器詳解
  • Python pytest裝飾器總結(實例詳解)
  • Python裝飾器的應用場景及實例用法
  • Python 的lru_cache裝飾器使用簡介
  • Python 中@lazyprop 裝飾器的用法

標簽:陽泉 金華 臨汾 日照 雙鴨山 克拉瑪依 貴州 赤峰

巨人網絡通訊聲明:本文標題《python基礎之裝飾器詳解》,本文關鍵詞  python,基礎,之,裝飾,器,詳解,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《python基礎之裝飾器詳解》相關的同類信息!
  • 本頁收集關于python基礎之裝飾器詳解的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 在线免费观看污片| 马交配视频| 久久久久精品国产片| 午夜视频免费| 色鬼视频app| 麻豆精产国品一二三产区风险| 欧美24p| 国产精品日韩欧美一区二区三区| 国产精品亚洲AV三区爱咪桃| 日韩精品在线免费观看| 女人被躁的疯狂呻吟文| 国产精品久久久亚洲456| 无码毛片一区二区三区视频免费播放 | 野狼全球第一中文精品社区| 自制安慰小玩具| 日本少妇极品熟妇人妻| 欧美4K超高清HD| 羞羞视频在线观看免费观看| 好男人好资源在线观看免费官网| 青娱乐激情| 超h+高h| 黑亚当在线观看高清完整1080英语| 日韩人妻无码一区二区三区中文| 我被5个男人躁一夜不收我怎么办| 国产在线观看福利片| 六个教练好大在用力深些| 一区二区不卡在线观看| 国产对白叫床清晰在线播放| 欧洲另类一二三四区| 扒开粉嫩小泬白浆20p| 国产AV无码亚洲AV毛片菲菲| 色狠狠色狠狠综合一区| 韩国理伦片在线理伦韩国| 欧美综合色| 好热好涨帮帮我| 金瓶春梦| 人乳喂奶HD无中字| 亚洲精品粉嫩小泬18p| 500柠檬AV福利在线导航| 欲乱女人的奶水| 中文字幕成人在线|