-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator.py
More file actions
52 lines (41 loc) · 1006 Bytes
/
Copy pathdecorator.py
File metadata and controls
52 lines (41 loc) · 1006 Bytes
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
import functools
# 构建不带参数的装饰器
def logging(func):
@functools.wraps(func)
def decorator(*args, **kwargs):
print("%s called" % func.__name__)
result = func(*args, **kwargs)
print("%s end" % func.__name__)
return result
return decorator
# 使用装饰器
@logging
def test01(a, b):
print("in function test01, a=%s, b=%s" % (a, b))
return 1
# 使用装饰器
@logging
def test02(a, b, c=1):
print("in function test02, a=%s, b=%s, c=%s" % (a, b, c))
return 1
# test01(1, 2)
# test02(1, 2)
# 装饰器实例: 函数缓存
def funccache(func):
cache = {}
@functools.wraps(func)
def _inner(*args):
print(args)
if args not in cache:
cache[args] = func(*args)
else:
print("in cache")
return cache[args]
return _inner
# 使用装饰器
@funccache
def test08(a, b, c):
# 其他复杂或耗时计算
return a + b + c
test08(1, 2, 3)
test08(1, 2, 3)