一、裝飾器
目錄
1、裝飾器定義
2、裝飾器原則
3、裝飾器練習
4、裝飾器高級版
二、
1、定義:
本質是函式,就是為其他函式附加功能
2、原則:
1、不能修改被裝飾的函式的源代碼
2、不能修改被修飾的函式的呼叫方式
3、練習
1 def demo1(func): 2 def doca(*args,**kwargs): 3 func(*args,**kwargs) 4 print('我是顯示幕') 5 return doca 6 7 @demo1 8 def b1(): 9 print('源代碼1') 10 11 @demo1 12 def b2(name,age): 13 print('源代碼2',name,age) 14 15 16 b1() 17 b2('alex',23)
4、高級版
1、需求是,test1、test2、test3假如是三種不同平臺要登錄,所驗證的方式不一樣
1 user,pwd='123','123' 2 def auto(auto_type): 3 def worapps_type(func): 4 def worapps(*args,**kwargs): 5 if auto_type == 'ldouc': 6 username=input('輸入用戶名:') 7 password=input('輸入密碼:') 8 if username == user and password == pwd: 9 print('登錄成功!我是第一種登錄方式') 10 return func(*args,**kwargs) 11 else: 12 exit() 13 elif auto_type == 'ldap': 14 print('我是第二種登錄方式') 15 return worapps 16 return worapps_type 17 18 19 20 def text1(): 21 print('我是text1') 22 23 @auto(auto_type='ldouc') 24 def text2(): 25 print('我是text2') 26 return 'text2回傳值' 27 28 @auto(auto_type='ldap') 29 def text3(): 30 print('我是text3') 31 32 33 text1() 34 text2() 35 text3()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/164054.html
標籤:Python
上一篇:機器學習第3章分類
下一篇:python copy
