1 #!/usr/bin/env python 2 #encoding: utf-8 3 def start_info(): 4 print ('電視劇開頭曲.......') 5 print ('開始唱歌.......') 6 def end_info(): 7 print ('電視劇結束曲.......') 8 9 def filter(start_info,end_info): #接收倆函數 10 def outer(main_fun): #接收裝飾的函數 11 def app(*argv,**kwargs): #接收裝飾的函數的參數 12 print('******************************') 13 start_info() 14 main_fun(*argv,**kwargs)#接收裝飾的函數的參數 15 end_info() 16 print('******************************') 17 return app 18 return outer 19 #先把函數傳進去,然后在用里面的函數裝飾 20 #傳函數的裝飾器必須有三個def ,第一個是接受函數,第二個是裝飾函數的,返回第三個函數對象 21 # 把需要裝飾的函數重新定義,然后調用調用 22 #1: filter(start_info,end_info): 23 #2: @outer -> one_info = outer(one_info) 24 @filter(start_info,end_info) #這里傳入倆個函數 25 def one_info(name,info): 26 print ('this is one') 27 print('wolcome to tv %s .......' % (name)) 28 print('wolcome to tv %s .......' % (info)) 29 30 @filter(start_info,end_info) 31 def two_info(name,info): 32 print('this is two') 33 print('wolcome to tv %s .......' % (name)) 34 print('wolcome to tv %s .......' % (info)) 35 36 @filter(start_info,end_info) 37 def three_info(name,info): 38 print('this is three') 39 print('wolcome to tv %s .......' % (name)) 40 print('wolcome to tv %s .......' % (info)) 41 42 if __name__ == "__main__": 43 print('三國演義三部曲開始。。。。。。。。') 44 print('第一部。。。。。。。。。。。。。。') 45 one_info('三國電視劇第一部', '三國大戰') 46 47 print('第二部。。。。。。。。。。。。。。') 48 two_info('三國電視劇第二部', '三國英雄') 49 50 print('第三部。。。。。。。。。。。。。。') 51 three_info('三國電視劇第三部', '三國鼎力')
1 #!/usr/bin/env python 2 #encoding: utf-8 3 def start_info(): 4 print ('電視劇開頭曲.......') 5 print ('開始唱歌.......') 6 def end_info(): 7 print ('電視劇結束曲.......') 8 9 def outer(main_fun): #接收裝飾的函數 10 def app(*argv,**kwargs): #接收裝飾的函數的參數 11 print('******************************') 12 start_info() 13 main_fun(*argv,**kwargs)#接收裝飾的函數的參數 14 end_info() 15 print('******************************') 16 return app 17 18 #1: @outer -> one_info = outer(one_info) 19 @outer 20 def one_info(name,info): 21 print ('this is one') 22 print('wolcome to tv %s .......' % (name)) 23 print('wolcome to tv %s .......' % (info)) 24 25 @outer 26 def two_info(name,info): 27 print('this is two') 28 print('wolcome to tv %s .......' % (name)) 29 print('wolcome to tv %s .......' % (info)) 30 31 @outer 32 def three_info(name,info): 33 print('this is three') 34 print('wolcome to tv %s .......' % (name)) 35 print('wolcome to tv %s .......' % (info)) 36 37 if __name__ == "__main__": 38 print ('三國演義三部曲開始。。。。。。。。') 39 print ('第一部。。。。。。。。。。。。。。') 40 one_info('三國電視劇第一部','三國大戰') 41 42 print('第二部。。。。。。。。。。。。。。') 43 two_info('三國電視劇第二部','三國英雄') 44 45 print('第三部。。。。。。。。。。。。。。') 46 three_info('三國電視劇第三部','三國鼎力')