0%

装饰器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# -*- coding:utf-8 -*-
# @Time :2022/6/24 9:42
# @SOFTWARE :python基础

# 装饰器:在不改变原有代码的前提下,为原函数拓展新功能
"""
@符号 装饰器的标识符:
(1)自动把下面修饰的原函数当成参数传递给装饰器
(2)把返回的新函数去替换原函数
"""
"""
# (1)装饰器的原型
def tuozhan(_func):
def newfunc():
print("你好,中国")
_func()
print("你好,上海")
return newfunc
def func():
print("你好,世界")

func = tuozhan(func) # => func = newfunc
func()
"""

# (2) @符号的使用
"""
def tuozhan(_func):
def newfunc():
print("你好,中国")
_func()
print("你好,上海")
return newfunc

@tuozhan
def func():
print("你好,北京")

func()
"""
# (3)装饰器的嵌套
def tuozhan1(_func):
def newfunc():
print("你好,中国1")
_func()
print("你好,上海2")
return newfunc

def tuozhan2(_func):
def newfunc():
print("你好,广东3")
_func()
print("你好,深圳4")
return newfunc

@tuozhan2
@tuozhan1
def func():
print("你好,北京5")

func() # 31524 func => newfunc(tuohzan1) => newfunc(tuozhan2)


# (4)用装饰器拓展带有参数的原函数
print("===========================")
def tuozhan3(func):
def newfunc(who,where):
print("你好,老王")
func(who,where)
print("你好,小明")
return newfunc

@tuozhan3
def func(who,where):
print("{}在{}打游戏".format(who,where))

func("老王","网吧")


# (5)用装饰器拓展带有参数和返回值的原函数
print("<=============================>")
def tuozhan4(func):
def newfunc(*args,**kwargs):
print("在网吧打游戏")
res = func(*args,**kwargs)
print("洗浴中心捏脚")
return res
return newfunc

@tuozhan4
def func(*args,**kwargs):
lst = []
for i in args:
print(i)

for k,v in kwargs.items():
strvar = k +":" + v
lst.append(strvar)
return lst
lst = func("小明","25","男",name = "老王",age = "16",sex = "男")
print(lst)

# (6)使用类装饰器
print("<=============================>")
class TuoZhan():
def tuozhan(func):
def newfunc():
print("上午打游戏")
func2()
print("下午去捏脚")
return newfunc

# @TuoZhan.tuozhan
def func():
print("你好,我想睡觉")

# func()

# @TuoZhan()
def func2():
print("你好,很高兴认识你")
# func2()

# (7)带有参数的函数装饰器
def outer(num):
def tuozhan(_func):
def newfunc1(self):
print("你好,中国")
_func(self)
print("你好,世界")
def newfunc2(self):
print("你好,北京")
_func(self)
print("你好,上海")

def newfunc3():
print("你好,广州")
_func()
print("你好,江苏")
if num == 1:
return newfunc1
elif num == 2:
return newfunc2
elif num == 3:
return "你好,山西"
return tuozhan

class MyClass():

@outer(1)
def func1(self):
print("向前一小步")

@outer(2)
def func2(self):
print("文明一大步")

@outer(3)
def func3(self):
print("请瞄准后发射")

obj = MyClass()
obj.func1()
obj.func2()
res = obj.func3
print(res)

# (8)带有参数的类装饰器
"""
参数1:给修饰的类添加成员属性和方法
参数2:把类中的run方法变成属性
"""
print("<=============================>")

class TuoZhan():

ad = "倚天屠龙记"

def money(self):
print("神雕侠侣")

def __init__(self,num):
self.num = num

def __call__(self,cls):
print(cls) # MyClass
if self.num == 1:
return self.tuozhan1(cls)
elif self.num == 2:
pass
elif self.num == 3:
pass
def tuozhan1(self,cls):
def newfunc():
cls.money = TuoZhan.money
cls.ad = TuoZhan.ad
return cls()
return newfunc





@TuoZhan(1) # @obj => MyClass => obj(MyClass)
class MyClass():
def run(self):
return "天龙八部"

obj = MyClass()
print(obj.ad)
obj.money()