装饰器的形成过程
import time
def func(): # 定义一个函数
time.sleep(0.01)
print(‘hello world!‘)
def timer(f): # 一个闭包函数,接收一个函数,也叫做装饰器函数
def inner():
start = time.time()
f() # 被装饰函数
end = time.time()
print(end - start)
return inner
func = timer(func)
func()
20
1
import time
2
3
4
def func(): # 定义一个函数
5
time.sleep(0.01)
6
print(‘hello world!‘)
7
8
9
def timer(f): # 一个闭包函数,接收一个函数,也叫做装饰器函数
10
def inner():
11
start = time.time()
12
f() # 被装饰函数
13
end = time.time()
14
print(end - start)
15
return inner
16
17
18
func = timer(func)
19
func()
20
装饰器的定义:把接收函数作为参数,以及返回内部函数的函数称之为装饰器函数
语法糖的概念
import time
def timer(f): # 一个闭包函数,接收一个函数,也叫做装饰器函数
def inner():
start = time.time()
f() # 被装饰函数
end = time.time()
print(end - start)
return inner
@timmer # 语法糖 @装饰器函数名
def func(): # 被装饰函数 # 定义一个函数 # func = timer(func)
time.sleep(0.01)
print(‘hello world!‘)
# func = timer(func)
func() # 在调用的时候只要调用func()即可!这才是装饰器
20
1
import time
2
3
4
def timer(f): # 一个闭包函数,接收一个函数,也叫做装饰器函数
5
def inner():
6
start = time.time()
7
f() # 被装饰函数
8
end = time.time()
9
print(end - start)
10
return inner
11
12
13
@timmer # 语法糖 @装饰器函数名
14
def func(): # 被装饰函数 # 定义一个函数 # func = timer(func)
15
time.sleep(0.01)
16
print(‘hello world!‘)
17
18
19
# func = timer(func)
20
func() # 在调用的时候只要调用func()即可!这才是装饰器
再次进化 (被装饰的函数有返回值)
55
1
import time
2
3
4
def timer(f):
5
def inner():
6
start = time.time()
7
f()
8
end = time.time()
9
print(end - start)
10
return inner
11
12
13
@timer
14
def func():
15
time.sleep(0.01)
16
print(‘hello world!‘)
17
return ‘pontoon on the way!’
18
19
ret = func()
20
print(ret)
21
22
>>>hello world!
23
0.010217905044555664
24
None # 打印的结果是None,而不是pontoon on the way!,因为这里接收的是inner函数里面的返回值,而inner函数里面并没有返回值
25
26
27
-------------------------------------------------------------------------------------------------------------------------------------------------------
28
import time
29
30
31
def timer(f):
32
def inner():
33
start = time.time()
34
ret = f() # 得到传递过来函数的返回值
35
end = time.time()
36
print(end - start)
37
return ret # 返回
38
return inner
39
40
41
@timer
42
def func():
43
time.sleep(0.01)
44
print(‘hello world!‘)
45
return ‘pontoon on the way!‘
46
47
48
# func = timer(func)
49
ret = func()
50
print(ret)
51
52
>>>hello world!
53
0.010081291198730469
54
pontoon on the way!
55
究极进化 (被装饰的函数有参数)
import time
def timer(f):
def inner(*args, **kwargs):
start = time.time()
ret = f(*args, **kwargs) # 得到传递过来函数的返回值
end = time.time()
print(end - start)
return ret # 返回
return inner
@timer
def func(a, b):
time.sleep(0.01)
print(‘hello world!‘)
return ‘pontoon on the way!‘
# func = timer(func)
ret = func(1, 2) # 这里相当于执行inner()
print(ret)
-----------------------------------------------------------------------------------------------
# 简化之
import time
def wrapper(f):
def inner(*args, **kwargs):
ret = f(*args, **kwargs) # 得到传递过来函数的返回值
return ret # 返回
return inner
@wrapper
def func(a, b):
time.sleep(0.01)
print(‘hello world!‘)
return ‘pontoon on the way!‘
# func = timer(func(1, 2))
ret = func(1, 2) # 这里相当于执行inner()
print(ret)
至此简化版的装饰器OK了!
49
1
import time
2
3
4
def timer(f):
5
def inner(*args, **kwargs):
6
start = time.time()
7
ret = f(*args, **kwargs) # 得到传递过来函数的返回值
8
end = time.time()
9
print(end - start)
10
return ret # 返回
11
return inner
12
13
14
@timer
15
def func(a, b):
16
time.sleep(0.01)
17
print(‘hello world!‘)
18
return ‘pontoon on the way!‘
19
20
21
# func = timer(func)
22
ret = func(1, 2) # 这里相当于执行inner()
23
print(ret)
24
25
-----------------------------------------------------------------------------------------------
26
# 简化之
27
import time
28
29
30
def wrapper(f):
31
def inner(*args, **kwargs):
32
ret = f(*args, **kwargs) # 得到传递过来函数的返回值
33
return ret # 返回
34
return inner
35
36
37
@wrapper
38
def func(a, b):
39
time.sleep(0.01)
40
print(‘hello world!‘)
41
return ‘pontoon on the way!‘
42
43
44
# func = timer(func(1, 2))
45
ret = func(1, 2) # 这里相当于执行inner()
46
print(ret)
47
48
49
至此简化版的装饰器OK了!
装饰器的作用
在不修改原函数及其调用方式的情况下对原函数的功能进行拓展
原则:开放闭合原则
开放:对拓展时开放的
封装:对修改是封闭的
装饰器的固定模式
def wrapper(f):
def inner(*args, **kwargs):
ret = f(*args, **kwargs) # 被装饰的函数要有接收的参数并且有返回值!
return ret
return inner
@wrapper
def qqxing():
print(123)
print(qqxing())
x
1
def wrapper(f):
2
def inner(*args, **kwargs):
3
ret = f(*args, **kwargs) # 被装饰的函数要有接收的参数并且有返回值!
4
return ret
5
return inner
6
7
8
@wrapper
9
def qqxing():
10
print(123)
11
12
13
print(qqxing())
装饰器的进阶
1、带参数的装饰器
2、多个装饰器装饰同一个函数
span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror-line::-moz-selection, .CodeMirror-line > span::-moz-selection, .CodeMirror-line > span > span::-moz-selection { background: #d7d4f0; }.cm-searching {background: #ffa; background: rgba(255, 255, 0, .4);}.cm-force-border { padding-right: .1px; }@media print { .CodeMirror div.CodeMirror-cursors {visibility: hidden;}}.cm-tab-wrap-hack:after { content: ""; }span.CodeMirror-selectedtext { background: none; }.CodeMirror-activeline-background, .CodeMirror-selected {transition: visibility 0ms 100ms;}.CodeMirror-blur .CodeMirror-activeline-background, .CodeMirror-blur .CodeMirror-selected {visibility:hidden;}.CodeMirror-blur .CodeMirror-matchingbracket {color:inherit !important;outline:none !important;text-decoration:none !important;}.CodeMirror-sizer {min-height:auto !important;}
-->
li {list-style-type:decimal;}.wiz-editor-body ol.wiz-list-level2 > li {list-style-type:lower-latin;}.wiz-editor-body ol.wiz-list-level3 > li {list-style-type:lower-roman;}.wiz-editor-body blockquote {padding: 0 12px;}.wiz-editor-body blockquote > :first-child {margin-top:0;}.wiz-editor-body blockquote > :last-child {margin-bottom:0;}.wiz-editor-body img {border:0;max-width:100%;height:auto !important;margin:2px 0;}.wiz-editor-body table {border-collapse:collapse;border:1px solid #bbbbbb;}.wiz-editor-body td,.wiz-editor-body th {padding:4px 8px;border-collapse:collapse;border:1px solid #bbbbbb;min-height:28px;word-break:break-word;box-sizing: border-box;}.wiz-hide {display:none !important;}
-->
原文地址:https://www.cnblogs.com/pontoon/p/10238331.html