Python---裝飾器詳解
# 定義:
本質上是一個函數。作用是用來裝飾另一個函數(即被裝飾函數),為被裝飾函數加入功能。前提是不能改變被裝飾函數的原始碼和呼叫方式。這樣的一個函數稱為裝飾器。
解析:
下面我們話不多說,直接用程式碼說明。下面是一個函數。
b=1+2
程式輸出:
————————
3
————————
現在我要為這個函數增加一個解釋性的句子,如下,我們可以寫一個裝飾器:
1 #原函数 2 def add(): 3 a=1+2 4 print(a) 5 #装饰器 6 def decorator(func): 7 def warpper(): 8 print("1+2的结果是:") 9 func()10 return warpper11 #注意此句 12 add=decorator(add)13 #调用函数14 add()
程式輸出:
##——————————#1+2的結果是:##3
——————————
這樣我們就成功的達成了我們的目的。這裡要注意第12行的這一句,這一句是將add這個函數物件傳入了decorator()函數,返回的是一個新函數變量,這個新函數物件又重新賦值給add,這樣就可以保證不改變被裝飾函數的呼叫方式不變。在Python語法中有一個更優雅的方式可以取代第十二行的語句。如下:
1 #装饰器 2 def decorator(func): 3 def warpper(): 4 print("1+2的结果是:") 5 func() 6 return warpper 7 8 #add=decorator(add) 9 #原函数10 @decorator#换成@符号11 def add():12 a=1+213 print(a)14 #调用函数15 add()
在被裝飾函數前面直接加上「@xxx」(xxx為裝飾器函數名稱)即可
被裝飾函數有參數怎麼辦? 如果被裝飾函數有參數呢?該怎們班?不用擔心,我們可以用不定參數的形式來收集參數。實例程式碼如下:
1 def decorator(func): 2 def warpper(*args,**kwargs): 3 print("相加的结果是:") 4 func(*args,**kwargs) 5 return warpper 6 7 @decorator 8 def add(x,y): 9 a=x+y10 print(a)11 12 add(2,3)
程序输出: —————————————————— 相加的结果是: 5 ——————————————————
下面寫一個頁面驗證的裝飾器。
大家知道有些網站的一部分頁面是要求使用者登入之後才可以存取的,例如下面的三個函數(分別代表三個頁面):
1 def index():2 print("welcome to the index page")3 def home():4 print("welcome to the home page")5 def bbs():6 print("welcome to the bbs page")7 return "I am the return contents"
1 username,passwd="jack","abc123"#模拟一个已登录用户 2 def decorator(func): 3 def warpper(*args,**kwargs): 4 Username=input("Username:").strip() 5 password=input("Password:").strip() 6 if username==Username and passwd==password: 7 print("Authenticate Success!") 8 func(*args,**kwargs) 9 else:10 exit("Username or password is invalid!")11 return warpper12 13 def index():14 print("welcome to the index page")15 @decorator16 def home():17 print("welcome to the home page")18 @decorator19 def bbs():20 print("welcome to the bbs page")21 return "I am the return contents"22 23 index()24 home()25 bbs()
#程式結果:
————————
welcome to the index page #index頁面未驗證直接可以登錄
Username:jackPassword:abc123
Authenticate Success! #登入的而情形#"#nname#home#homecname#home page# :jack #以且使用者名稱為錯誤的情形
#Password:123
Username or password is invalid!
————————
我們注意到bbs()是有回傳值的,如果我們把上述程式碼的最後一句(第25行)改為「print(bbs() )」之後再看看他的輸出結果:
————————
welcome to the index page
Username:jackPassword:abc123
Authenticate Success!welcome to the home page
Username:jack
Password:abc123
Authenticate Success!
welcome to the bbs page##
#回傳值能麼成None了? ? ?
————————
What happened! bbs()的回傳值印出來竟然是None。怎麼會這樣?這樣的話不就改變了被裝飾函數的原始碼了嗎?怎樣才能解決呢?
1 username,passwd="jack","abc123"#模拟一个已登录用户 2 def decorator(func): 3 def warpper(*args,**kwargs): 4 Username=input("Username:").strip() 5 password=input("Password:").strip() 6 if username==Username and passwd==password: 7 print("Authenticate Success!") 8 return func(*args,**kwargs)#在这里加一个return就行了 9 else:10 exit("Username or password is invalid!")11 return warpper12 13 def index():14 print("welcome to the index page")15 @decorator16 def home():17 print("welcome to the home page")18 @decorator19 def bbs():20 print("welcome to the bbs page")21 return "I am the return contents"22 23 index()24 home()25 bbs()
如图加上第8行的return就可以解决了。下面我们在看看改后的程序输出:
————————
welcome to the index page
Username:jack
Password:abc123
Authenticate Success!
welcome to the home page
Username:jack
Password:abc123
Authenticate Success!
welcome to the bbs page
I am the return contents #bbs()的返回值得到了正确的返回
——-——————
好了,返回值的问题解决了.
既然装饰器是一个函数,那装饰器可以有参数吗?
答案是肯定的。我们同样可以给装饰器加上参数。比如还是上面的三个页面函数作为例子,我们可以根据不同页面的验证方式来给程序不同的验证,而这个验证方式可以以装饰器的参数传入,这样我们就得在装饰器上在嵌套一层函数 了:
1 username,passwd="jack","abc123"#模拟一个已登录用户 2 def decorator(auth_type): 3 def out_warpper(func): 4 def warpper(*args,**kwargs): 5 Username=input("Username:").strip() 6 password=input("Password:").strip() 7 if auth_type=="local": 8 if username==Username and passwd==password: 9 print("Authenticate Success!")10 return func(*args,**kwargs)11 else:12 exit("Username or password is invalid!")13 elif auth_type=="unlocal":14 print("HERE IS UNLOCAL AUTHENTICATE WAYS")15 return warpper16 return out_warpper17 18 def index():19 print("welcome to the index page")20 @decorator(auth_type="local")21 def home():22 print("welcome to the home page")23 @decorator(auth_type="unlocal")24 def bbs():25 print("welcome to the bbs page")26 return "I am the return contents"27 28 index()29 home()30 bbs()
输出:
————————
welcome to the index page
Username:jack
Password:abc123
Authenticate Success!
welcome to the home page
Username:jack
Password:abc123
HERE IS UNLOCAL AUTHENTICATE WAYS
————————
可见,程序分别加入了第2行和第16行和中间的根据auth_type参数的判断的相关内容后, 就解决上述问题了。对于上面的这一个三层嵌套的相关逻辑,大家可以在 pycharm里头加上断点,逐步调试,便可发现其中的道理。
总结
要想学好迭代器就必须理解一下三条:
1.函数即变量(即函数对象的概念)
2.函数嵌套
3.函数式编程
以上是python裝飾器是什麼意思?如何使用python裝飾器?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Python3.6環境下加載Pickle文件報錯:ModuleNotFoundError:Nomodulenamed...

如何解決jieba分詞在景區評論分析中的問題?當我們在進行景區評論分析時,往往會使用jieba分詞工具來處理文�...

如何使用正則表達式匹配到第一個閉合標籤就停止?在處理HTML或其他標記語言時,常常需要使用正則表達式來�...

攻克Investing.com的反爬蟲策略許多人嘗試爬取Investing.com(https://cn.investing.com/news/latest-news)的新聞數據時,常常�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

禪工作室 13.0.1
強大的PHP整合開發環境