首页 > 代码库 > 装饰器的应用-装饰器带参数和不带参数
装饰器的应用-装饰器带参数和不带参数
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(‘三国电视剧第三部‘,‘三国鼎力‘)
装饰器的应用-装饰器带参数和不带参数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。