forked from xianhu/LearnPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_decorator.py
209 lines (156 loc) · 4.53 KB
/
python_decorator.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# _*_ coding: utf-8 _*_
"""
python_decorator.py by xianhu
"""
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
# 构建带参数的装饰器
def params_chack(*types, **kwtypes):
def _outer(func):
@functools.wraps(func)
def _inner(*args, **kwargs):
result = [isinstance(_param, _type) for _param, _type in zip(args, types)]
assert all(result), "params_chack: invalid parameters"
result = [isinstance(kwargs[_param], kwtypes[_param]) for _param in kwargs if _param in kwtypes]
assert all(result), "params_chack: invalid parameters"
return func(*args, **kwargs)
return _inner
return _outer
# 使用装饰器
@params_chack(int, (list, tuple))
def test03(a, b):
print("in function test03, a=%s, b=%s" % (a, b))
return 1
# 使用装饰器
@params_chack(int, str, c=(int, str))
def test04(a, b, c):
print("in function test04, a=%s, b=%s, c=%s" % (a, b, c))
return 1
# 在类的成员方法中使用装饰器
class ATest(object):
@params_chack(object, int, str)
def test(self, a, b):
print("in function test of ATest, a=%s, b=%s" % (a, b))
return 1
# 同时使用多个装饰器
@logging
@params_chack(int, str, (list, tuple))
def test05(a, b, c):
print("in function test05, a=%s, b=%s, c=%s" % (a, b, c))
return 1
# 构建不带参数的装饰器类
class Decorator(object):
def __init__(self, func):
self.func = func
return
def __call__(self, *args, **kwargs):
print("%s called" % self.func.__name__)
result = self.func(*args, **kwargs)
print("%s end" % self.func.__name__)
return result
# 使用装饰器
@Decorator
def test06(a, b, c):
print("in function test06, a=%s, b=%s, c=%s" % (a, b, c))
return 1
# 构建带参数的装饰器类
class ParamCheck(object):
def __init__(self, *types, **kwtypes):
self.types = types
self.kwtypes = kwtypes
return
def __call__(self, func):
@functools.wraps(func)
def _inner(*args, **kwargs):
result = [isinstance(_param, _type) for _param, _type in zip(args, self.types)]
assert all(result), "params_chack: invalid parameters"
result = [isinstance(kwargs[_param], self.kwtypes[_param]) for _param in kwargs if _param in self.kwtypes]
assert all(result), "params_chack: invalid parameters"
return func(*args, **kwargs)
return _inner
# 使用装饰器
@ParamCheck(int, str, (list, tuple))
def test07(a, b, c):
print("in function test06, a=%s, b=%s, c=%s" % (a, b, c))
return 1
# 装饰器实例: 函数缓存
def funccache(func):
cache = {}
@functools.wraps(func)
def _inner(*args):
if args not in cache:
cache[args] = func(*args)
return cache[args]
return _inner
# 使用装饰器
@funccache
def test08(a, b, c):
# 其他复杂或耗时计算
return a + b + c
# 使用Python自带的装饰器 @property
class Person(object):
def __init__(self):
self._name = None
return
def get_name(self):
print("get_name")
return self._name
def set_name(self, name):
print("set_name")
self._name = name
return
name = property(fget=get_name, fset=set_name, doc="person name")
# 使用Python自带的装饰器 @property
class People(object):
def __init__(self):
self._name = None
self._age = None
return
@property
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
return
@property
def age(self):
return self._age
@age.setter
def age(self, age):
assert 0 < age < 120
self._age = age
return
# 类静态方法和类方法
class A(object):
var = 1
def func(self):
print(self.var)
return
@staticmethod
def static_func():
print(A.var)
return
@classmethod
def class_func(cls):
print(cls.var)
cls().func()
return