#裝飾器定義:本質就是函數,功能是爲其餘函數添加附加功能python
#原則:
1、不修改被修飾函數的源代碼
2、不修改被修飾函數的調用方式 裝飾器他人的器具,本事能夠是任意可調用對象,被裝飾者也能夠是任意可調用對象。 #強調裝飾器的原則: 1、不修改被裝飾對象的源代碼 2、不修改被裝飾對象的調用方式 #裝飾器的目標: 在遵循1和2原則的前提下,爲被裝飾的對象添加新功能
裝飾器=高階函數+函數嵌套+閉包
高階函數的定義:
1、函數接收的參數是一個函數名 2、函數的返回值是一個函數名 三、知足上述條件任意一個,均可稱之爲高階函數
def father(name):
print('from father %s' %name) def son(): print('from the son') def grandson(): print('from the grandson') grandson() son() father('朱銳')
def father(name):
print('from father %s' %name) def son(): print('from the son') def grandson(): print('from the grandson') grandson() son() father('朱銳') ''' 閉包 ''' def father(name): def son(): # name='simon1' print('個人爸爸是%s' %name) def grandson(): print('個人爺爺是%s' %name) grandson() son() father('simon')
import time
def timmer(func): def wrapper(): # print(func) start_time=time.time() func() #就是在運行test() stop_time=time.time() print('運行時間是%s' %(stop_time-start_time)) return wrapper @timmer #語法糖,這個是重點 def test(): time.sleep(3) print('test函數運行完畢') # res=timmer(test) #返回的是wrapper的地址 # res() #執行的是wrapper() # test=timmer(test) #返回的是wrapper的地址 # test() #執行的是wrapper() test() ''' 語法糖 ''' # @timmer #就至關於 test=timmer(test)
#未加返回值
import time
def timmer(func): def wrapper(): # print(func) start_time=time.time() func() #就是在運行test() stop_time=time.time() print('運行時間是%s' %(stop_time-start_time)) return 123 return wrapper @timmer #語法糖 def test(): time.sleep(3) print('test函數運行完畢') return '這是test的返回值' res=test() #就是在運行wrapper print(res) 運行結果以下: C:\Python35\python3.exe G:/python_s3/day20/加上返回值.py test函數運行完畢 運行時間是3.000171661376953 123
#加上返回值
import time
def timmer(func): def wrapper(): # print(func) start_time=time.time() res=func() #就是在運行test() ##主要修改這裏1 stop_time=time.time() print('運行時間是%s' %(stop_time-start_time)) return res ##修改這裏2 return wrapper @timmer #語法糖 def test(): time.sleep(3) print('test函數運行完畢') return '這是test的返回值' res=test() #就是在運行wrapper print(res) 運行結果以下: C:\Python35\python3.exe G:/python_s3/day20/加上返回值.py test函數運行完畢 運行時間是3.000171661376953 這是test的返回值
import time
def timmer(func): def wrapper(name,age): #加入參數,name,age # print(func) start_time=time.time() res=func(name,age) ##加入參數,name,age stop_time=time.time() print('運行時間是%s' %(stop_time-start_time)) return res return wrapper @timmer #語法糖 def test(name,age): #加入參數,name,age time.sleep(3) print('test函數運行完畢,名字是【%s】,年齡是【%s】' % (name,age)) return '這是test的返回值' res=test('simon',18) #就是在運行wrapper print(res)
使用可變長參數代碼以下:達到的效果是傳參靈活閉包
import time
def timmer(func): def wrapper(*args,**kwargs): #test('simon',18) args=('simon') kwargs={'age':18} # print(func) start_time=time.time() res=func(*args,**kwargs) #就是在運行test() func(*('simon'),**{'age':18}) stop_time=time.time() print('運行時間是%s' %(stop_time-start_time)) return res return wrapper @timmer #語法糖 def test(name,age): time.sleep(3) print('test函數運行完畢,名字是【%s】,年齡是【%s】' % (name,age)) return '這是test的返回值' def test1(name,age,gender): time.sleep(1) print('test函數運行完畢,名字是【%s】,年齡是【%s】,性別是【%s】' % (name,age,gender)) res=test('simon',18) #就是在運行wrapper print(res) test1('simon',18,'male')
#無參裝飾器 import time def timmer(func): def wrapper(*args,**kwargs): start_time=time.time() res=func(*args,**kwargs) stop_time=time.time() print('run time is %s' %(stop_time-start_time)) return res return wrapper @timmer def foo(): time.sleep(3) print('from foo') foo()
#有參裝飾器 def auth(driver='file'): def auth2(func): def wrapper(*args,**kwargs): name=input("user: ") pwd=input("pwd: ") if driver == 'file': if name == 'simon' and pwd == '123': print('login successful') res=func(*args,**kwargs) return res elif driver == 'ldap': print('ldap') return wrapper return auth2 @auth(driver='file') def foo(name): print(name) foo('simon')
#驗證功能裝飾器app
#驗證功能裝飾器 user_list=[ {'name':'simon','passwd':'123'}, {'name':'zhurui','passwd':'123'}, {'name':'william','passwd':'123'}, {'name':'zhurui1','passwd':'123'}, ] current_dic={'username':None,'login':False} def auth_func(func): def wrapper(*args,**kwargs): if current_dic['username'] and current_dic['login']: res=func(*args,**kwargs) return res username=input('用戶名:').strip() passwd=input('密碼:').strip() for user_dic in user_list: if username == user_dic['name'] and passwd == user_dic['passwd']: current_dic['username']=username current_dic['login']=True res=func(*args,**kwargs) return res else: print('用戶名或者密碼錯誤') # if username == 'simon' and passwd == '123': # user_dic['username']=username # user_dic['login']=True # res=func(*args,**kwargs) # return res # else: # print('用戶名或密碼錯誤') return wrapper @auth_func def index(): print('歡迎來到某寶首頁') @auth_func def home(name): print('歡迎回家%s' %name) @auth_func def shopping_car(name): print('%s購物車裏有[%s,%s,%s]' %(name,'餐具','沙發','電動車')) print('before----->',current_dic) index() print('after---->',current_dic) home('simon') # shopping_car('simon')
#帶參數驗證功能裝飾器函數
#帶參數驗證功能裝飾器 user_list=[ {'name':'simon','passwd':'123'}, {'name':'zhurui','passwd':'123'}, {'name':'william','passwd':'123'}, {'name':'zhurui1','passwd':'123'}, ] current_dic={'username':None,'login':False} def auth(auth_type='filedb'): def auth_func(func): def wrapper(*args,**kwargs): print('認證類型是',auth_type) if auth_type == 'filedb': if current_dic['username'] and current_dic['login']: res = func(*args, **kwargs) return res username=input('用戶名:').strip() passwd=input('密碼:').strip() for user_dic in user_list: if username == user_dic['name'] and passwd == user_dic['passwd']: current_dic['username']=username current_dic['login']=True res = func(*args, **kwargs) return res else: print('用戶名或者密碼錯誤') elif auth_type == 'ldap': print('這玩意沒搞過,不知道怎麼玩') res = func(*args, **kwargs) return res else: print('鬼才知道你用的什麼認證方式') res = func(*args, **kwargs) return res return wrapper return auth_func @auth(auth_type='filedb') #auth_func=auth(auth_type='filedb')-->@auth_func 附加了一個auth_type --->index=auth_func(index) def index(): print('歡迎來到某寶主頁') @auth(auth_type='ldap') def home(name): print('歡迎回家%s' %name) # @auth(auth_type='sssssss') def shopping_car(name): print('%s的購物車裏有[%s,%s,%s]' %(name,'奶茶','妹妹','娃娃')) # print('before-->',current_dic) # index() # print('after--->',current_dic) # home('simon') shopping_car('simon')