-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test_decorator.py
72 lines (64 loc) · 1.52 KB
/
Test_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
# def tag(func):
# def wrapper(text):
# value = func(text)
# return "p" + value + "p"
# return wrapper
#
#
# def t(name):
# def decorator(func):
# def wrapper(text):
# value = func(text)
# return "<{name}>{value}</{name}>".format(name=name, value=value)
# return wrapper
# return decorator
#
# # @tag
# @t("abc")
# def my_upper(text):
# value = text.upper()
# return value
#
#
# def billion(func):
# def convert(x):
# y = func(x)
# y = y.split('.')
# y = y[0]
# return y[:] # 100,000,000 - billion
# return convert
#
# @billion
# def convert_num(x):
# return x
#
# if __name__ == '__main__':
# print(my_upper("hello"))
# print(convert_num('123.456'))
'''''示例8: 装饰器带类参数'''
class locker:
def __init__(self):
print("locker.__init__() should be not called.")
@staticmethod
def acquire():
print("locker.acquire() called.(这是静态方法)")
@staticmethod
def release():
print(" locker.release() called.(不需要对象实例)")
def deco(cls):
'''''cls 必须实现acquire和release静态方法'''
def _deco(func):
def __deco():
print("before %s called [%s]." % (func.__name__, cls))
cls.acquire()
try:
return func()
finally:
cls.release()
return __deco
return _deco
@deco(locker)
def myfunc():
print(" myfunc() called.")
myfunc()
myfunc()