七、Python-python基础(一)-- 装饰器

一、普通装饰器

装饰器原理

1: 执行outer函数,将@下面的函数名称,当做参数
2: 将outer函数的返回值重新赋值给f1

 1 def outer(func):
 2     def inner():
 3         return 'aa'
 4     return inner
 5 
 6 @outer
 7 def f1():
 8     return 'f1'
 9 
10 a = f1()
11 print(a)
12 
13 C:\Python35\python.exe D:/python/decode.py
14 aa

二、带参数装饰器

1:带2个参数

 1 def outer(func):
 2     def inner(a1,a2):
 3         print('123')
 4         ret = func(a1,a2)
 5         print('456')
 6         return ret
 7     return inner
 8 
 9 @outer
10 def index(a1,a2):
11     print('非常复杂')
12     return a1 + a2
13 
14 index(1,2)
15 
16 C:\Python35\python.exe D:/python/decode.py
17 123
18 非常复杂
19 456

2:多个变量,使用 *args **kwargs

 1 def outer(func):
 2     def inner(*args,**kwargs):
 3         print('123')
 4         ret = func(*args,**kwargs)
 5         print('456')
 6         return ret
 7     return inner
 8 
 9 @outer
10 def index1(a1,a2):
11     print('2个函数')
12     return a1 + a2
13 
14 
15 @outer
16 def index2(a1,a2,a3):
17     print('3个函数')
18     return a1 + a2
19 
20 index1(1,2)
21 index2(1,2,3)
22 
23 
24 
25 C:\Python35\python.exe D:/python/decode.py
26 123
27 2个函数
28 456
29 123
30 3个函数
31 456

 

三、多个装饰器

举例:

 1 def outer_0(func):
 2  2     def inner(*args, **kwargs):
 3  3         print('最外出层装饰器')
 4  4         ret = func(*args, **kwargs)
 5  5         print('执行完外层装饰器')
 6  6         return ret
 7  7     return inner
 8  8 
 9  9 
10 10 def outer(func):
11 11     def inner(*args, **kwargs):
12 12         print('里层装饰器')
13 13         ret = func(*args, **kwargs)
14 14         print('执行完里层装饰器')
15 15         return ret
16 16     return inner
17 17 
18 18 
19 19 @outer_0
20 20 @outer
21 21 def index(a,b):
22 22     print('我是原函数')
23 23     return a + b
24 24 
25 25 t = index(1,2)
26 26 print(t)
27 27 
28 28 C:\Python35\python.exe D:/python/decode.py
29 29 最外出层装饰器
30 30 里层装饰器
31 31 我是原函数
32 32 执行完里层装饰器
33 33 执行完外层装饰器
34 34 3

流程:

 1 def outer(func):
 2     def inner(*args,**kwargs):
 3         print('123')
 4         ret = func(*args,**kwargs)
 5         print('456')
 6         return ret
 7     return inner
 8 
 9 @outer
10 def index1(a1,a2):
11     print('2个函数')
12     return a1 + a2
13 
14 
15 # @outer
16 # def index2(a1,a2,a3):
17 #     print('3个函数')
18 #     return a1 + a2
19 
20 index1(1,2)
21 index2(1,2,3)
22 
23 """
24 1:  解释器从上向下执行。在内存先创建outer函数。 函数内部未执行。
25 2: 创建index函数。
26 3: 执行index 和outer的联合。
27 4: 执行outer函数,将index作为参数传递给outer。
28 5: 在传递参数给outer的时候,创建func函数。所以outer 也指向index 。
29 6:  执行outer内的inner。创建inner函数。
30 7: 在函数inner内部,func 指向原来的index函数。
31 8: inner函数是新的index函数。
32 
33 执行步骤:
34 1:执行index()
35 2:执行outer 内部函数的inner。
36 3:在inner 内部,执行print(123)调。用原来index。
37 4:执行原来的index。
38 5:执行完毕原来的index 后,返回inner 内部。
39 6:执行inner 剩下的步骤。
40 7:inner 执行完毕,返回相关的返回值。
41 
42 执行index()的时候,执行的是新的index,也就是outer 内部的inner
43 """
44 
45 #
46 def outer_0(func):
47     def inner(*args, **kwargs):
48         print('最外出层装饰器')
49         ret = func(*args, **kwargs)
50         print('执行完外层装饰器')
51         return ret
52     return inner
53 
54 
55 def outer(func):
56     def inner(*args, **kwargs):
57         print('里层装饰器')
58         ret = func(*args, **kwargs)
59         print('执行完里层装饰器')
60         return ret
61     return inner
62 
63 
64 @outer_0
65 @outer
66 def index(a,b):
67     print('我是原函数')
68     return a + b
69 
70 t = index(1,2)
71 print(t)
72 
73 """
74 内部流程:
75 1: 解释器从上向下执行,将outer_0,outer 均在内存中创建函数。
76 2:创建index函数。
77 3:绑定@outer和index 作为@outer_0的参数。传递给outer_0 。 outer_0 会把inner 重新赋值给绑定的@outer 和index
78 4: 所以outer_0 内部的inner = @outer的inner的合并
79 5:@outer和inner的合并,本质上是 ouer内部 inenr函数重新赋值给index。
80 6:所以相当于给outer内部的inner 添加上了@outer_0的装饰器。或者说 将 outer_0 的inner 重新赋给 outer 的inner
81  也就重新赋值给了index
82 
83 
84 执行步骤:
85 1:在执行index(),先执行最外层装饰器的inner函数。
86 2:执行outer_0 的 inner函数内部的func函数。
87 3:上步骤的func指的是内层装饰器的iner函数。
88 4:执行outer的inner函数内部的 func函数。
89 5:步骤4的func 指的是原函数index
90 6:执行原函数index
91 7:执行完原源函数index后,回到步骤4,执行outer 的inner 函数 剩下的步骤。print 和return操作。
92 8:执行完outer 的inner函数后,回到步骤4,执行outer_0 的inner函数,剩下的步骤。print 和return 操作。
93 9:执行完毕
94 """
95 
96 """

 

 

练习

  1 # # # f1 = open('a.txt', 'w+')
  2 # # # a = f1.tell()
  3 # # # #f1.write('22')
  4 # # # print(a)
  5 # # # data = f1.read()
  6 # # # #f1.write('北京')
  7 # # #
  8 # # # a = f1.tell()
  9 # # # print(a)
 10 # # # f1.close()
 11 # # # print(data)
 12 # #
 13 # # import copy
 14 # # dict1 = {'k1':123}
 15 # # li = [11,22,33,44,dict1]
 16 # #
 17 # # # print(id(li))
 18 # # cpli1 = li
 19 # # cpli2 = copy.copy(li)
 20 # # cpli3 = copy.deepcopy(li)
 21 # #
 22 # # # print(id(cpli1))
 23 # # # print(id(cpli2))
 24 # # # print(id(cpli3))
 25 # #
 26 # # print(id(li[0]))
 27 # # print(id(li[4]))
 28 # #
 29 # # print(id(cpli2[0]))
 30 # # print(id(cpli2[4]))
 31 # #
 32 # #
 33 # # print(id(cpli3[0]))
 34 # # print(id(cpli3[4]))
 35 #
 36 #
 37 #
 38 # #lambda
 39 #
 40 # # a =  print('2') if 1 < 2 else print('1')
 41 #
 42 # def fun1(a,b): #普通函数
 43 #     return a > b
 44 #
 45 # t = lambda a,b: a > b #lambda表达式
 46 #
 47 # print(t(1,2)) #调用lambda表达式
 48 #
 49 #
 50 # bin
 51 
 52 
 53 
 54 #
 55 # print(int('0b110',base = 2))
 56 # print(int('0o110',base = 8))
 57 # print(int('0x11',base = 16))
 58 
 59 # print(chr(80))
 60 #
 61 # li = []
 62 # print(dir(li))
 63 
 64 
 65 # print(divmod(22,7))
 66 # print(eval('1 + 4'))
 67 # exec('print(2) if 1 >2  else print(1)')
 68 #
 69 # a = 'i am {0}, {1}'
 70 # print(a.format('yan',10))
 71 
 72 # a = 'a'
 73 #
 74 # print(help(list))
 75 
 76 # a = [11]
 77 # print(isinstance(a,list))
 78 
 79 
 80 
 81 
 82 # a = locals()
 83 # print(a)
 84 
 85 
 86 # a = 'strings'
 87 #
 88 # next(a)
 89 # dict1 = {'asdasfdsfas':1}
 90 # i = hash('asdasfdsfas')
 91 # print(i)
 92 
 93 
 94 # a = iter([11,22,33,44])
 95 # print(next(a))
 96 # print(next(a))
 97 
 98 
 99 # a = ['11','22','abc','ABC', 'db','中文']
100 # li = sorted(a)
101 #
102 # for item in li:
103 #     print(item)
104 
105 # import random
106 # temp = ''
107 #
108 # for i in range(6):
109 #      a = random.randrange(0,4)
110 #      if a==2 or a ==4:
111 #          rad1 = random.randrange(65,91)
112 #          c1 = chr(rad1)
113 #          temp = temp + c1
114 #      else:
115 #          rad2 = random.randrange(0,9)
116 #          c1 = str(rad2)
117 #          temp = temp + c1
118 # print(temp)
119 
120 # #只读
121 # f = open('a.txt','r')
122 # data = f.read()
123 # print(data)
124 #
125 # #只写
126 # f = open('a.txt','w')
127 # f.write('aaa')
128 # print(data)
129 
130 
131 
132 #x模式
133 # f = open('b.txt','x')
134 # f.write('aaa')
135 
136 
137 #a模式
138 
139 # f = open('a.txt','r')
140 # data = f.read()
141 # print(type(data))
142 #
143 #
144 # f = open('a.txt','rb')
145 # data = f.read()
146 # print(type(data))
147 
148 
149 # f = open('b.txt','wb')
150 # f.write(bytes('aaa',encoding = 'utf-8'))
151 
152 
153 
154 #r+
155 #
156 # f = open('a.txt','r+')
157 # print(f.tell()) #得到当前指针位置
158 # data = f.read()
159 # print(type(data))
160 # print(data)
161 # print(f.tell()) # 得到读取后的指针位置
162 # f.write('中国人')
163 # print(f.tell()) # 得到write后的指针位置
164 # f.seek(2)  # 调整指针位置到2个字节。
165 # data1 = f.read(5) #读取5个字符
166 # print(data1)
167 
168 
169 #w+
170 # f = open('a.txt','a+')
171 # print(f.tell()) #得到当前指针位置
172 # data = f.read() #此处读不到任何内容,因为r+ open的时候指针到最后
173 # f.seek(0)  # #调整指针位置到0
174 # f.write('add') #但是追加还是在文件末尾
175 # f.seek(0)
176 # data1 = f.read()
177 # print(data1)
178 # f.close()
179 
180 
181 # f = open('a.txt','r+')
182 # data = f.readline()
183 # print(data)
184 # data1 = f.readline()
185 # print(data1)
186 # data1 = f.readline()
187 # print(data1)
188 
189 
190 
191 
192 # f = open('a.txt','r+')
193 # print(f.tell())
194 # print(f.seek(2))
195 # f.truncate()
196 # data = f.read()
197 # print(data)
198 
199 #
200 #
201 # f = open('a.txt','r+')
202 # for line in f:
203 #     print(line)
204 #
205 #
206 # f = open('a.txt','r+')
207 # for line in f:
208 #     print(line)
209 # f.readline
210 #
211 #
212 # with open('a.txt','r+') as f:
213 #     f.read()
214 #
215 #
216 # with open('a.txt','r+') as f1, open('b.txt','a+') as f2:
217 #     for lines in f1:
218 #         f2.write(lines)
219 
220 
221 # li = [11,22,9,5,44,39]
222 # count1 = 0
223 # count2 = 0
224 # for i in range(len(li)-1):
225 #     count2 = count2 +1
226 #     for j in range(len(li)-1):
227 #         temp = li[j]
228 #         count1 = count1 +1
229 #         if li[j] > li[j+1]:
230 #             li[j] = li[j+1]
231 #             li[j+1] = temp
232 #
233 # for item in li:
234 #     print(item)
235 #
236 # print('循环 %d 趟,排序 %d 次'%(count2,count1))
237 
238 
239 #
240 # def fib(a,b):
241 #     if a > 100 :
242 #         return
243 #     print(a)
244 #     print(b)
245 #     c = a + b
246 #     fib(b,c)
247 #
248 # fib(1,2)
249 #
250 #
251 #
252 # def fe(i):
253 #     if i == 5:
254 #         return 10
255 #
256 #     r = fe(i+1)
257 #
258 #     return r
259 #
260 # t = fe(1)
261 # print(t)
262 
263 
264 
265 # def outer(func):
266 #     def inner():
267 #         return 'aa'
268 #     return inner
269 #
270 # @outer
271 # def f1():
272 #     return 'f1'
273 #
274 # a = f1()
275 # print(a)
276 
277 
278 def outer(func):
279     def inner(*args,**kwargs):
280         print('123')
281         ret = func(*args,**kwargs)
282         print('456')
283         return ret
284     return inner
285 
286 @outer
287 def index1(a1,a2):
288     print('2个函数')
289     return a1 + a2
290 
291 
292 # @outer
293 # def index2(a1,a2,a3):
294 #     print('3个函数')
295 #     return a1 + a2
296 
297 index1(1,2)
298 index2(1,2,3)
299 
300 """
301 1:  解释器从上向下执行。在内存先创建outer函数。 函数内部未执行。
302 2: 创建index函数。
303 3: 执行index 和outer的联合。
304 4: 执行outer函数,将index作为参数传递给outer。
305 5: 在传递参数给outer的时候,创建func函数。所以outer 也指向index 。
306 6:  执行outer内的inner。创建inner函数。
307 7: 在函数inner内部,func 指向原来的index函数。
308 8: inner函数是新的index函数。
309 
310 执行步骤:
311 1:执行index()
312 2:执行outer 内部函数的inner。
313 3:在inner 内部,执行print(123)调。用原来index。
314 4:执行原来的index。
315 5:执行完毕原来的index 后,返回inner 内部。
316 6:执行inner 剩下的步骤。
317 7:inner 执行完毕,返回相关的返回值。
318 
319 执行index()的时候,执行的是新的index,也就是outer 内部的inner
320 """
321 
322 #
323 def outer_0(func):
324     def inner(*args, **kwargs):
325         print('最外出层装饰器')
326         ret = func(*args, **kwargs)
327         print('执行完外层装饰器')
328         return ret
329     return inner
330 
331 
332 def outer(func):
333     def inner(*args, **kwargs):
334         print('里层装饰器')
335         ret = func(*args, **kwargs)
336         print('执行完里层装饰器')
337         return ret
338     return inner
339 
340 
341 @outer_0
342 @outer
343 def index(a,b):
344     print('我是原函数')
345     return a + b
346 
347 t = index(1,2)
348 print(t)
349 
350 """
351 内部流程:
352 1: 解释器从上向下执行,将outer_0,outer 均在内存中创建函数。
353 2:创建index函数。
354 3:绑定@outer和index 作为@outer_0的参数。传递给outer_0 。 outer_0 会把inner 重新赋值给绑定的@outer 和index
355 4: 所以outer_0 内部的inner = @outer的inner的合并
356 5:@outer和inner的合并,本质上是 ouer内部 inenr函数重新赋值给index。
357 6:所以相当于给outer内部的inner 添加上了@outer_0的装饰器。或者说 将 outer_0 的inner 重新赋给 outer 的inner
358  也就重新赋值给了index
359 
360 
361 执行步骤:
362 1:在执行index(),先执行最外层装饰器的inner函数。
363 2:执行outer_0 的 inner函数内部的func函数。
364 3:上步骤的func指的是内层装饰器的iner函数。
365 4:执行outer的inner函数内部的 func函数。
366 5:步骤4的func 指的是原函数index
367 6:执行原函数index
368 7:执行完原源函数index后,回到步骤4,执行outer 的inner 函数 剩下的步骤。print 和return操作。
369 8:执行完outer 的inner函数后,回到步骤4,执行outer_0 的inner函数,剩下的步骤。print 和return 操作。
370 9:执行完毕
371 """
View Code

 

posted @ 2016-06-04 14:19  咖啡茶  阅读(94)  评论(0)    收藏  举报