裝飾器總結(jié)

import time

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、成都微信小程序、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了大安免費建站歡迎大家使用!

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

裝飾器元祖:1、不能修改被裝飾的函數(shù)源代碼。 2、不能修改不裝飾的函數(shù)的調(diào)用方式。

裝飾器 函數(shù)內(nèi)存地址不變,函數(shù)名變了其結(jié)果不變?。。?/h2>

def timmer(func): # 這就是裝飾器!
def warpper(*args,*kwargs):
start_time = time.time()
func(
args,**kwargs)
stop_time = time.time()
print("the func run time is %s" % (stop_time - start_time))
return warpper

@timmer #函數(shù)的內(nèi)存地址賦予(把timmer這個裝飾器的內(nèi)存地址給調(diào)用他的函數(shù))
def test():
time.sleep(3)
print("test1")

test()

實現(xiàn)裝飾器的知識儲備:

1.函數(shù)即“變量”

2.高階函數(shù):1、把一個函數(shù)名當(dāng)做實參傳給另外一個函數(shù)(在不修改被裝飾函數(shù)

源代碼的情況下為其添加功能)

2、返回值中包含函數(shù)名(不修改函數(shù)的調(diào)用方式)

3.函數(shù)嵌套

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

先聲明定義再調(diào)用?。?!

#1
def fff():
print("llllllll")
xxx()

def xxx():
print("ccococococ")

fff()

#2
def xxx():
print("ccococococ")

def fff():
print("llllllll")
xxx()

fff()

#3 調(diào)用xxx的時候后,xxx沒有先定義!
def fff():
print("llllllll")
xxx()

fff()

def xxx():
print("ccococococ")

實現(xiàn)不改變源代碼附加功能??!

def timmer(func): # 這就是裝飾器!
def warpper():
start_time = time.time()
func()
stop_time = time.time()
print("the func run time is %s" % (stop_time - start_time))
return warpper

def test():
time.sleep(3)
print("test1")

timmer(test)

實現(xiàn)不改變源代碼和調(diào)用方式附加功能?。?!

def timmer(func): # 這就是裝飾器!
def warpper():
start_time = time.time()
func()
stop_time = time.time()
print("the func run time is %s" % (stop_time - start_time))
return warpper

@timmer #函數(shù)的內(nèi)存地址賦予(把timmer這個裝飾器的內(nèi)存地址給調(diào)用他的函數(shù))
def test():
time.sleep(3)
print("test1")

test()

帶參數(shù)給裝飾器!

def timmer(func): # 這就是裝飾器!
def warpper(*args,*kwargs):
start_time = time.time()
func(
args,**kwargs)
stop_time = time.time()
print("the func run time is %s" % (stop_time - start_time))
return warpper

@timmer #函數(shù)的內(nèi)存地址賦予(把timmer這個裝飾器的內(nèi)存地址給調(diào)用他的函數(shù))
def test(): # @timmer=test
time.sleep(3)
print("test1")
test("alex")

最高級的裝飾器

user,keypass="hy","123"
def xxxx(func):
def wapper(*args, *kwargs):
username = input("username:").strip()
pasword = input("pssword:").strip()
if user == username and keypass == pasword:
print("welcome to logging---。。。。")
rrs = func(
args, **kwargs)
return rrs
else:
print("username or passord is no")
return wapper

def index():
print("welcome to index")
return 0@xxxx
br/>@xxxx
print("welcome to home ")
return "alex is pig " # 函數(shù)的返回值需要輸出,home()調(diào)用不會輸出返回值。@xxxx
br/>@xxxx
print("welcome to bbs")

index()
print(home())
bbs()

最終級的裝飾器

user,passwd="alex","123"
def auth(aupe):
def out_warpper(func):
def wrapper(*args, *kwargs):
if aupe == "local":
username = input("username:").strip()
password = input("password:").strip()
if user == username and passwd == password:
print("\033[32;1muser has passed authentication\033[0m")
res = func(
args, **kwargs)
return res
else:
print("\033[32;1mInvalid username or password\033[0m")
elif aupe == "country":
print("搞毛線ldap,不會")
else:print("city 歡迎你")
return wrapper
return out_warpper@auth(aupe="city")
br/>@auth(aupe="city")
print("welcome to city!")@auth(aupe="country")
br/>@auth(aupe="country")
print("welcome to country!!")@auth(aupe="local")
br/>@auth(aupe="local")
print("welcome to home!!! %name")
acct=acc-name
return "from there",acct
city()
country()
local()
print(local())# 函數(shù)的返回值需要print函數(shù)的調(diào)用(local()),或者賦值給一個變量,輸出那個變量!

文章名稱:裝飾器總結(jié)
URL分享:http://bm7419.com/article42/psdoec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化網(wǎng)站建設(shè)、手機網(wǎng)站建設(shè)App設(shè)計、軟件開發(fā)、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)