裝飾器: 本質(zhì)就是函數(shù)颜懊,為其他函數(shù)添加附加功能
裝飾器原則:
不修改被裝飾函數(shù)的源代碼
不修改被裝飾函數(shù)的調(diào)用方式
裝飾器=高階函數(shù) + 函數(shù)嵌套 +閉包
1.高階函數(shù)
定義:
函數(shù)接收的參數(shù)是一個函數(shù)名
函數(shù)的返回值是一個函數(shù)名
滿足上述任意一個條件的函數(shù)即為高階函數(shù)
高階函數(shù)示例
1 def foo():
2 print("hello")
3
4
5 # 函數(shù)名作為參數(shù)
6 def func_1(func):
7 print("heiheihei")
8 func()
9 print("world")
10
11
12 # 函數(shù)名作為返回值
13 def func_2(func):
14 print("return value: %s" % func)
15 return func
16
17 # 函數(shù)名作為參數(shù)和返回值
18 def func_3(func):
19 print("from func_3")
20 return func
21
22 func_1(foo)
23 print('----------------')
24 func_2(foo)
25 print('----------------')
26 f = func_3(foo)
27 f() # f是func_3的返回值沸呐,即為函數(shù)foo的函數(shù)地址挑庶,f()執(zhí)行函數(shù)foo輸出“hello”
28
29 >>heiheihei
30 hello
31 world
32 ----------------
33 return value: <function foo at 0x00000276CAAA3E18>
34 ----------------
35 from func_3
36 hello
使用高階函數(shù)實現(xiàn)在不改變函數(shù)調(diào)用方式的前提下妆档,增加函數(shù)的功能
1 import time
2
3 # 為foo函數(shù)增加函數(shù)運行時間計時功能
4
5 def foo():
6 time.sleep(2)
7 print("function foo running")
8
9 def timer(func):
10 start_time = time.time()
11 func()
12 stop_time = time.time()
13
14 print("running time %s" % (start_time - stop_time))
15
16 return func
17
18 foo = timer(foo)
19
20 foo()
21
22 >>function foo running
23 running time -2.0002999305725098
24 function foo running
25
26 # 以foo()的形式運行函數(shù)foo观腊,實現(xiàn)了不改變函數(shù)調(diào)用方式運行函數(shù)
27 # 但運行過程中多執(zhí)行了一次foo名船,
28 # 第一次執(zhí)行是timer函數(shù)中的func()
29 # 第二次執(zhí)行是timer函數(shù)返回了foo函數(shù)名诈茧,最后運行foo()再次運行了foo原函數(shù)
根據(jù)以上示例可以得出單純使用高階函數(shù)無法完全實現(xiàn)在不改變函數(shù)調(diào)用方式的基礎(chǔ)下增加函數(shù)的功能
2. 函數(shù)嵌套
定義:在函數(shù)里面嵌套定義函數(shù)
1 def country(name):
2 print('country is %s' %name)
3 def province():
4 print('guangdong')
5 def city():
6 print('shenzhen')
7 city()
8 province()
9
10 country('china')
11
12 >>country is china
13 guangdong
14 shenzhen
3. 閉包
定義:如果在一個內(nèi)部函數(shù)里,對在外部作用域(但不是在全局作用域)的變量進行引用纺念,那么內(nèi)部函數(shù)就被認(rèn)為是閉包(closure)
1 def country(name):
2 print('country is %s' %name)
3 def province():
4 print('province is %s' % name)
5 def city():
6 print('city is %s' % name)
7 city()
8 province()
9
10 country('china')
province()函數(shù)就是一個閉包
4. 使用高階函數(shù)+函數(shù)嵌套+閉包實現(xiàn)為函數(shù)增加功能
import time
def timer(func):
def inner():
start_time = time.time()
func()
stop_time = time.time()
print("running time is %s" % (stop_time- start_time))
return inner
def foo():
print("foo function")
time.sleep(2)
f = timer(foo) # f就是timer函數(shù)的返回值--inner函數(shù)的函數(shù)指針
f() # f加括號執(zhí)行f函數(shù)
>>foo function
running time is 2.000217914581299
上述示例實現(xiàn)了為foo函數(shù)增加函數(shù)運行時間計時的功能贝椿,如果將f改為foo,則實現(xiàn)了不改變函數(shù)的調(diào)用方式同時增加了函數(shù)的功能陷谱,如下所示
1 import time
2
3 def timer(func):
4 def inner():
5 start_time = time.time()
6 func()
7 stop_time = time.time()
8 print("running time is %s" % (stop_time- start_time))
9 return inner
10
11 def foo():
12 print("foo function")
13 time.sleep(2)
14
15 foo = timer(foo)
16
17 foo()
18
19 >> foo function
20 running time is 2.000906229019165
21
22 # 最后執(zhí)行的foo函數(shù)相比于最初定義的foo函數(shù)烙博,實現(xiàn)了增加函數(shù)運行時間計時的功能
到此基本實現(xiàn)了在不改變函數(shù)的調(diào)用方式以及函數(shù)源碼的前提下瑟蜈,實現(xiàn)了增加函數(shù)功能,但還需要做一步函數(shù)賦值的操作渣窜,如果其他函數(shù)需要怎加同樣的功能铺根,還要對其他函數(shù)做賦值操作,這是很麻煩的乔宿。
使用裝飾器可以避免這樣的麻煩位迂,只要在添加功能的目標(biāo)函數(shù)前添加一行代碼:@功能函數(shù)。實現(xiàn)方法如下所示
1 import time
2
3 def timer(func):
4 def inner():
5 start_time = time.time()
6 func()
7 stop_time = time.time()
8 print("running time is %s" % (stop_time- start_time))
9 return inner
10
11 @timer
12 def foo():
13 print("foo function")
14 time.sleep(2)
15
16 foo()
17
18 >>foo function
19 running time is 2.000242233276367
5. 獲取被裝飾函數(shù)的返回值
1 import time
2
3 def timer(func):
4 def inner():
5 start_time = time.time()
6 ret = func() # 執(zhí)行func函數(shù)得到函數(shù)返回值详瑞,賦值給ret
7 stop_time = time.time()
8 print("running time is %s" % (stop_time- start_time))
9 return ret # inner的返回值為ret
10 return inner # timer的返回值為inner函數(shù)名
11
12 @timer
13 def foo():
14 print("foo function")
15 time.sleep(2)
16 return "return value foo"
17
18 ret = foo()
19 # 首先假設(shè)foo = A
20 # 執(zhí)行foo() <==> 首先執(zhí)行A = timer(foo)掂林,然后執(zhí)行A()
21 # timer(foo)的返回值為inner,即A = inner
22 # 執(zhí)行A() <==> 執(zhí)行inner()
23 # 執(zhí)行inner()過程中執(zhí)行inner函數(shù)體中的所有代碼蛤虐,得到最初定義的foo函數(shù)的返回值“return value foo”
24 # 即執(zhí)行A()得到返回值“return value foo”
25 # ret = A() 將A()的返回值賦值給ret党饮, 即使用ret=foo()可以得到最初定義的foo函數(shù)的返回值
26 print(ret)
27
28 >>foo function
29 running time is 2.0001463890075684
30 return value foo
6. 被修飾函數(shù)有參數(shù)
1 import time
2
3 def timer(func):
4 def inner(*args, **kwargs):
5 start_time = time.time()
6 ret = func(*args, **kwargs) # 執(zhí)行func函數(shù)得到函數(shù)返回值肝陪,賦值給ret
7 stop_time = time.time()
8 print("running time is %s" % (stop_time- start_time))
9 return ret # inner的返回值為ret
10 return inner # timer的返回值為inner函數(shù)名
11
12 @timer
13 def foo(name):
14 print("usr name is %s" % name)
15 time.sleep(2)
16 return "return value foo"
17
18 ret = foo("Jack")
19 # 首先假設(shè)foo = A
20 # 執(zhí)行foo() <==> 首先執(zhí)行A = timer(foo)驳庭,然后執(zhí)行A()
21 # timer(foo)的返回值為inner,即A = inner
22 # 執(zhí)行A() <==> 執(zhí)行inner()
23 # 執(zhí)行inner()過程中執(zhí)行inner函數(shù)體中的所有代碼氯窍,
24 # 執(zhí)行到func函數(shù)時饲常,即執(zhí)行被修飾函數(shù)foo(name),foo有參數(shù)狼讨,則func也需要參數(shù)
25 # func的參數(shù)需要從A()的參數(shù)中獲得贝淤,即inner的參數(shù)就是func的參數(shù)
26 # 由于參數(shù)是不定的,可以為函數(shù)添加參數(shù) *args政供, **kwargs
27
28 print(ret)
29
30 >> usr name is Jack
31 running time is 2.0003693103790283
32 return value foo
7. 帶參數(shù)裝飾器
1 import time
2
3 def type_check(*args, **kwargs):
4 file_type = kwargs["file_type"]
5 print("file type %s" %file_type)
6
7 def timer(func):
8 def inner(*args, **kwargs):
9 start_time = time.time()
10 ret = func(*args, **kwargs) # 執(zhí)行func函數(shù)得到函數(shù)返回值播聪,賦值給ret
11 stop_time = time.time()
12 print("running time is %s" % (stop_time- start_time))
13 return ret # inner的返回值為ret
14 return inner # timer的返回值為inner函數(shù)名
15
16 return timer
17
18 @type_check(file_type = "mysql")
19 def foo(name):
20 print("usr name is %s" % name)
21 time.sleep(2)
22 return "return value foo"
23
24 ret = foo("Jack")
25 # type_check加括號首先執(zhí)行type_check(),獲取type_check的參數(shù)
26 # foo <==> A = type_check(), A得到timer返回值
27 # 執(zhí)行foo() <==> timer()
28 # 相比于無參裝飾器布隔,有參裝飾器在無參裝飾器的基礎(chǔ)上套一層函數(shù)來傳參
29
30 print(ret)
31
32 >>file type mysql
33 usr name is Jack
34 running time is 2.00007963180542
35 return value foo
8. 多個裝飾器
1 def outer_0(func):
2 def inner(*args, **kwargs):
3 print('3.5')
4 ret = func(*args, **kwargs)
5 return ret
6 return inner
7
8 def outer(func):
9 def inner(*args, **kwargs):
10 print('123')
11 ret = func(*args, **kwargs)
12 print('456')
13 return ret
14 return inner
15
16 @outer_0
17 @outer
18 def index(a1, a2):
19 print('complex')
20 return a1 + a2
21
22 print(index(1, 2))
23
24
25 >>3.5
26 >>123
27 >>complex
28 >>456
29 >>3