-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_decoration.py
executable file
·85 lines (61 loc) · 1.56 KB
/
test_decoration.py
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
import logging
import json
'''不同使用裝飾器的方法'''
def loop(func):
def new_func(*args, **kwargs):
func(*args, **kwargs)
func(*args, **kwargs)
func(*args, **kwargs)
return new_func
@loop
def test0():
print("hi")
# step1
def test1():
def use_logging(func):
logging . warn(" %s is running" % func . __name__)
func()
def foo():
print('i am foo')
use_logging(foo)
# step2
def test2():
def use_logging(func):
def wrapper():
logging . warn(" %s is running" % func . __name__)
return func() # 把foo當做參數傳遞進來時,執行func()就相當於執行foo()
return wrapper
def foo():
print('i am foo')
# 因為裝飾器use_logging(foo)返回的時函數對象wrapper,這條語句相當於foo = wrapper
foo = use_logging(foo)
foo() # 執行foo()就相當於執行wrapper()
# step3
def test3():
def use_logging(func):
def wrapper():
logging . warn(" %s is running" % func . __name__)
return func()
return wrapper
@use_logging
def foo():
print("i am foo")
foo()
def test4():
class Foo (object):
def __init__(self, func):
self . _func = func
def __call__(self):
print('class decorator runing')
self . _func()
print('class decorator ending')
@Foo
def bar():
print('bar')
bar()
if __name__ == "__main__":
# test0()
# test1()
# test2()
# test3()
test4()