带参数和不带参数装饰器区别

本文通过三国电视剧的例子展示了Python装饰器的基本用法及其在代码结构中的应用。通过定义不同的装饰器来模拟电视剧的开始和结束曲,实现了对多个函数的一致性增强。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/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('三国电视剧第三部','三国鼎力')
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值