最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python基礎(chǔ)之裝飾器詳解

 更新時(shí)間:2021年04月19日 09:56:09   作者:思想流浪者  
這篇文章主要介紹了python基礎(chǔ)之裝飾器詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有一定的幫助,需要的朋友可以參考下

一、前言

裝飾器:本質(zhì)就是函數(shù),功能是為其他函數(shù)添加附加功能

原則:

  •     1、不修改被修飾函數(shù)的源代碼
  •     2、不修改被修飾函數(shù)的調(diào)用方式

裝飾器 = 高階函數(shù) + 函數(shù)嵌套 + 閉包

二、高階函數(shù)

高階函數(shù)定義:

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

test 函數(shù)是高階函數(shù),接受了一個(gè)foo 作為參數(shù)

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

timer 是一個(gè)高階函數(shù),這個(gè)函數(shù)返回值是一個(gè)函數(shù)

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

三、函數(shù)嵌套

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

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

四、裝飾器

實(shí)現(xiàn)一個(gè)計(jì)算函數(shù)執(zhí)行時(shí)間的函數(shù)作為裝飾器,用來(lái)計(jì)算被裝飾函數(shù)的執(zhí)行時(shí)間并打印

import time
 
def timer(func):  # 實(shí)現(xiàn)一個(gè)計(jì)算函數(shù)執(zhí)行時(shí)間的函數(shù)作為裝飾器,用來(lái)計(jì)算被裝飾函數(shù)的執(zhí)行時(shí)間并打出
    def wrapper():
        start_time = time.time()
        func()
        stop_time = time.time()
        print("運(yùn)行時(shí)間: %s" % (stop_time - start_time))
    return wrapper
 
# def test():  # 不使用裝飾器的同等實(shí)現(xiàn)
#     time.sleep(3)
#     print("test sleep 3s")
#
# test = timer(test)  # 返回的是 wrapper 的地址
# test()  # 執(zhí)行的是 wrapper
 
 
@timer
def test():  # 裝飾器的實(shí)現(xiàn)
    time.sleep(3)
    print("test sleep 3s")
 
test()  # 執(zhí)行的是 wrapper
# 結(jié)果:
#     test sleep 3s
#     運(yùn)行時(shí)間: 3.000915050506592

4.1 被裝飾方法帶返回值

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

4.2 被裝飾方法帶參數(shù)

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

4.3 驗(yàn)證功能裝飾器

假如 index() 、home()、shopping_car() 三個(gè)方法都需要登錄后才能訪問(wèn)(無(wú)法訪問(wèn)時(shí)里面不輸入對(duì)應(yīng)內(nèi)容),正常情況下只需登錄一次,后面訪問(wèn)其他方法就無(wú)需再次登錄。

可以通過(guò)@auth_fun裝飾器進(jìn)行驗(yàn)證用戶是否登錄,如果沒(méi)有就讓用戶輸入賬號(hào)密碼,用戶賬號(hào)密碼正確的記錄當(dāng)前登錄的用戶,其他方法無(wú)需再次登錄。

# 用戶列表
user_list = [
    {'name': 'xu1', 'passwd': '123'},
    {'name': 'xu2', 'passwd': '123'},
    {'name': 'xu3', 'passwd': '123'},
    {'name': 'xu4', 'passwd': '123'},
]
# 當(dāng)前登錄的用戶
current_dic = {"username": None, "login": False}
 
 
# 驗(yàn)證用戶是否登錄的裝飾器
#   如果用戶沒(méi)有登錄,讓用戶輸入賬號(hào)密碼,校驗(yàn)通過(guò)記錄用戶狀態(tài)
def auth_fun(func):
    def wrapper(*args, **kwargs):
        if current_dic["username"] and current_dic['login']:
            res = func(*args, **kwargs)
            return res
        username = input("請(qǐng)輸入用戶名:")
        pw = input("請(qǐng)輸入密碼:")
        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("用戶沒(méi)有注冊(cè)!")
    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 已經(jīng)登錄,無(wú)需在輸入
shopping_car()  # index 已經(jīng)登錄,無(wú)需在輸入
# 結(jié)果:
#     請(qǐng)輸入用戶名:xu1
#     請(qǐng)輸入密碼:123
#     this is index
#     this is home page
#     this is shopping car

4.4 驗(yàn)證功能裝飾器——帶參數(shù)

 裝飾器帶參數(shù),最簡(jiǎn)單的操作就是可以對(duì)被裝飾的函數(shù)進(jìn)行區(qū)別處理。

# 用戶列表
user_list = [
    {'name': 'xu1', 'passwd': '123'},
    {'name': 'xu2', 'passwd': '123'},
    {'name': 'xu3', 'passwd': '123'},
    {'name': 'xu4', 'passwd': '123'},
]
# 當(dāng)前登錄的用戶
current_dic = {"username": None, "login": False}
 
"""
    注意:帶參數(shù)的裝飾器會(huì)比沒(méi)有帶參數(shù)的裝飾器多嵌套一層函數(shù)(多了auth)
        調(diào)用方式是 @auth(auth_type="type1"), 返回 auth_fun,
        也就是說(shuō) @auth(auth_type="type1")相當(dāng)于 @auth_fun
        但是 auth_fun 函數(shù)所在的嵌套作用域多了一個(gè) 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("請(qǐng)輸入用戶名:")
                pw = input("請(qǐng)輸入密碼:")
                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("用戶沒(méi)有注冊(cè)!")
            elif auth_type == "type2":
                print("不用授權(quán)直接登錄: type = {}".format(auth_type))
                res = func(*args, **kwargs)
                return res
            else:
                print("其他type沒(méi)有實(shí)現(xiàn)")
        return wrapper
    return auth_fun
 
 
"""
    auth_fun = @auth(auth_type="type1") 
    auth_fun 所在的嵌套與將有一個(gè) auth_type 變量
    然后通過(guò) @auth()方法返回的對(duì)象注解 index,相當(dāng)于 @auth_fun 注解index 方法,最后得到 wrapper 對(duì)象
"""
@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",這個(gè)方法無(wú)需登錄可以直接執(zhí)行
index()  # 注意:auth_type="type1",需要登錄
shopping_car()  # 注意:auth_type="type3",沒(méi)有做處理
# 結(jié)果:
#     不用授權(quán)直接登錄: type = type2
#     this is home page
#     請(qǐng)輸入用戶名:xu1
#     請(qǐng)輸入密碼:123
#     this is index
#     其他type沒(méi)有實(shí)現(xiàn)

到此這篇關(guān)于python基礎(chǔ)之裝飾器詳解的文章就介紹到這了,更多相關(guān)python裝飾器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何Tkinter模塊編寫Python圖形界面

    如何Tkinter模塊編寫Python圖形界面

    本文講解為何使用Tkinter而非PyQt,創(chuàng)建一個(gè)基本的Tkinter程序,模塊化Tkinter程序,希望對(duì)大家有幫助。
    2020-10-10
  • python光學(xué)仿真面向?qū)ο蠊鈱W(xué)元件類的實(shí)現(xiàn)

    python光學(xué)仿真面向?qū)ο蠊鈱W(xué)元件類的實(shí)現(xiàn)

    這篇文章主要為大家介紹了python光學(xué)仿真面向?qū)ο蠊鈱W(xué)元件類的實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-10-10
  • Python+Django實(shí)現(xiàn)接口測(cè)試工具的示例代嗎

    Python+Django實(shí)現(xiàn)接口測(cè)試工具的示例代嗎

    本文主要介紹了Python+Django實(shí)現(xiàn)接口測(cè)試工具,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Python3簡(jiǎn)單實(shí)例計(jì)算同花的概率代碼

    Python3簡(jiǎn)單實(shí)例計(jì)算同花的概率代碼

    這篇文章主要介紹了Python3簡(jiǎn)單實(shí)例計(jì)算同花的概率代碼,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-12-12
  • Python時(shí)間管理黑科技之datetime函數(shù)詳解

    Python時(shí)間管理黑科技之datetime函數(shù)詳解

    在Python中,datetime模塊是處理日期和時(shí)間的標(biāo)準(zhǔn)庫(kù),它提供了一系列功能強(qiáng)大的函數(shù)和類,用于處理日期、時(shí)間、時(shí)間間隔等,本文將深入探討datetime模塊的使用方法,感興趣的可以了解下
    2023-08-08
  • 如何用Python?實(shí)現(xiàn)景區(qū)安防系統(tǒng)

    如何用Python?實(shí)現(xiàn)景區(qū)安防系統(tǒng)

    本設(shè)計(jì)中,利用YOLO目標(biāo)檢測(cè)算法、Openpose姿態(tài)識(shí)別算法、deepsort跟蹤算法、MSCNN人群密度估計(jì)算法實(shí)現(xiàn)了火災(zāi)監(jiān)測(cè)、吸煙監(jiān)測(cè)、行為安全監(jiān)測(cè)、人群密度監(jiān)測(cè)、口罩率監(jiān)測(cè)、人員定位監(jiān)測(cè)六大功能,對(duì)Python?實(shí)現(xiàn)景區(qū)安防系統(tǒng)感興趣的朋友一起看看吧
    2022-07-07
  • matplotlib繪制多個(gè)子圖(subplot)的方法

    matplotlib繪制多個(gè)子圖(subplot)的方法

    這篇文章主要介紹了matplotlib繪制多個(gè)子圖(subplot)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • 在pycharm中使用matplotlib.pyplot 繪圖時(shí)報(bào)錯(cuò)的解決

    在pycharm中使用matplotlib.pyplot 繪圖時(shí)報(bào)錯(cuò)的解決

    這篇文章主要介紹了在pycharm中使用matplotlib.pyplot 繪圖時(shí)報(bào)錯(cuò)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • python簡(jiǎn)單爬蟲--get方式詳解

    python簡(jiǎn)單爬蟲--get方式詳解

    本篇文章介紹了python爬蟲中g(shù)et和post方法介紹以及cookie作用,對(duì)此有興趣的朋友學(xué)習(xí)下,希望能夠給你帶來(lái)幫助
    2021-09-09
  • Python max函數(shù)中key的用法及原理解析

    Python max函數(shù)中key的用法及原理解析

    最近有童鞋向小編求助怎么樣找到字符串中出現(xiàn)字?jǐn)?shù)最多的字符呢,其實(shí)最簡(jiǎn)單的處理方法是使用max函數(shù),max()函數(shù)用于獲得給定的可迭代對(duì)象中的最大值,關(guān)于Python max函數(shù)key用法跟隨小編一起通過(guò)本文學(xué)習(xí)下吧
    2021-06-06

最新評(píng)論

独山县| 车险| 岗巴县| 潜江市| 革吉县| 青龙| 启东市| 霍邱县| 松原市| 延庆县| 内丘县| 遵化市| 西林县| 五河县| 府谷县| 尉氏县| 喀喇沁旗| 新疆| 荃湾区| 高要市| 邯郸县| 铅山县| 哈巴河县| 安岳县| 仪征市| 清涧县| 沈阳市| 博兴县| 那坡县| 九龙县| 特克斯县| 石狮市| 修文县| 专栏| 万全县| 高淳县| 海盐县| 湖南省| 尖扎县| 正定县| 清徐县|